Subject: [xsl] XSL substrings From: Mike Stroud <stroudmw@xxxxxxxxx> Date: Thu, 12 Feb 2009 12:47:57 +0200 |
Hello Everyone, I'm having a bit of a problem using substrings in XSL. I found the following example on the internet: <?xml version="1.0" ?> <winelist> <wine> <winery>Lindeman's</winery> <product>Bin 65</product> <year>1998</year> <price>6.99</price> <binCode>15A-7</binCode> </wine> <wine> <winery>Benziger</winery> <product>Carneros</product> <year>1997</year> <price>7.55</price> <binCode>15C-5</binCode> </wine> <wine> <winery>Duckpond</winery> <product>Merit Selection</product> <year>1996</year> <price>14.99</price> <binCode>12D-1</binCode> </wine> </winelist> I then use the following XSL: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="binCode"> <productLocation> <row><xsl:value-of select="substring(text(),1,2)"/> </row> <shelf><xsl:value-of select="substring(.,3,1)"/> </shelf> <prodNum><xsl:value-of select="substring-after(text(),'-')"/> </prodNum> </productLocation> </xsl:template> </xsl:stylesheet> And I get this: <?xml version="1.0" encoding="UTF-16"?> Lindeman'sBin 6519986.99<productLocation> <row>15</row> <shelf>A</shelf> <prodNum>7</prodNum> </productLocation>BenzigerCarneros19977.55<productLocation> <row>15</row> <shelf>C</shelf> <prodNum>5</prodNum> </productLocation>DuckpondMerit Selection199614.99<productLocation> <row>12</row> <shelf>D</shelf> <prodNum>1</prodNum> </productLocation> So far, so good. However, my real-life example is slightly different. Here is a section of my XML source: <?xml version="1.0" encoding="UTF-8"?> <AddedParts NAME="AddedParts" TYPE="Unknown" STATUS="0"> <Part> <Number>0000000025</Number> <DIMENSION>T=4 10x1618</DIMENSION> </Part> <Part> <Number>0000000026</Number> <DIMENSION>T=40 101x16</DIMENSION> </Part> </AddedParts> What I want is to get "<Thickness>", which is the number after the "T=", "<Length>" which the number before the "x" and <Width>" which is the number after the "x". I've tried the following: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="AddedParts/Part"> <PartNo> <xsl:value-of select="Number"/> <xsl:if test="position() != last()"> <xsl:value-of select="','"/> </xsl:if> <xsl:if test="position()=last()"> <xsl:value-of select="','"/> </xsl:if> </PartNo> <Width> <xsl:value-of select="DIMENSION"/> <xsl:value-of select="substring-after(text(),'x')"/> <xsl:if test="position() != last()"> <xsl:value-of select="','"/> </xsl:if> <xsl:if test="position()=last()"> <xsl:value-of select="','"/> </xsl:if> </Width> </xsl:for-each> </xsl:template> </xsl:stylesheet> This is obviously intended to be in CSV format. What I'm getting for "<Width>" is the entire "<DIMENSION>" string "T=4 10x1618", not just the bit after the "x": "1618". My question is how can I extract Thickness, Length and Width? I can't use absolute positions because I have no idea how big or small the values will be. Many thanks, Mike.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XSLT materials in Spanish, G. Ken Holman | Thread | Re: [xsl] XSL substrings, Andrew Welch |
Re: [xsl] XSLT materials in Spanish, G. Ken Holman | Date | Re: [xsl] XSL substrings, Andrew Welch |
Month |