Guest moyo333 Posted September 19, 2007 Share Posted September 19, 2007 Hello,I have a quite tricky problem with xslt and xpathThe following elements:<itemB refs="F1,F2" />should be transformed into this:<otherItem>F1</otherItem><otherItem>F2</otherItem>The attribute "refs" of "item" was defined as a String in the XML Schema.Can anyone help me with this please. Link to comment Share on other sites More sharing options...
boen_robot Posted September 20, 2007 Share Posted September 20, 2007 That would be quite hard, if not impossible in XSLT 1.0 without using EXSLT's tokenize() extension function. In XSLT 2.0, there's a built in function for that, again called tokenize(). Link to comment Share on other sites More sharing options...
aalbetski Posted September 20, 2007 Share Posted September 20, 2007 I came up with I believe to be a solution that will handle any number of items in the refs attribute (F1,F2,F3, etc...). It uses a recursive template to process each item (up to the comma) and then passes the remaining value back to itself (after removing the part it had already processed). The last one, without an ending comma, is handled by the otherwise. Works fine under XSLT 1.0, no extensions needed <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="/"> <xsl:apply-templates select="//itemB"/> </xsl:template> <xsl:template match="itemB"> <xsl:call-template name="DoRefs"> <xsl:with-param name="refs" select="@refs" /> </xsl:call-template> </xsl:template> <xsl:template name="DoRefs"> <xsl:param name="refs" /> <xsl:choose> <xsl:when test="contains($refs,',')"> <otherItem><xsl:value-of select="substring-before($refs,',')"/></otherItem> <xsl:call-template name="DoRefs"> <xsl:with-param name="refs" select="substring-after($refs,',')"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <otherItem><xsl:value-of select="$refs"/></otherItem> </xsl:otherwise> </xsl:choose> </xsl:template></xsl:stylesheet> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.