Subject: RE: [xsl] XSL substrings From: "Ferry, Jeremy" <Jeremy_Ferry@xxxxxxxxxxxx> Date: Fri, 13 Feb 2009 11:45:47 -0500 |
I'd probably go the route of tokenizing, something like this... <xsl:variable name="dim" select="tokenize(DIMENSION, '[= x]')"/> <xsl:variable name="thickness" select="$dim[2]"/> <xsl:variable name="length" select="$dim[3]"/> <xsl:variable name="width" select="$dim[4]"/> Then you just use the variables by name or you could get rid of the variables and just reference the tokens by index. I personally prefer the readability of named variables. ~Jeremy -----Original Message----- From: Mike Stroud [mailto:stroudmw@xxxxxxxxx] Sent: Thursday, February 12, 2009 4:48 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: [xsl] XSL substrings 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] XSL substrings, David Carlisle | Thread | [xsl] separate white space and show, himanshu padmanabhi |
RE: [xsl] Wrapping content in a CDA, Michael Kay | Date | Re: [xsl] regex in XSLT 2.0: proble, David Carlisle |
Month |