Reputation: 1233
Hi,
I have a variable 'sortedKeywords' its value is '1#5#13-14#9-10-11#7#3'. While looping (and when a particular condition is met ) I want the first number followed by '#' to be removed from the variable.
XML:
<root>
<kwd-group>
<title>Keywords</title>
<u>ddd</u>
<kwd>ZBustard</kwd>
<u>, </u>
<kwd>Grassland conservation</kwd>
<u>, </u>
<kwd>Tonle Sap</kwd>
<u>, </u>
<kwd>Irrigated rice</kwd>
<kwd><it>bss</it></kwd>
<kwd><it>ggggbss</it></kwd>
<u>, </u>
<kwd>Habitat conversion</kwd>
<kwd><b>bold</b></kwd>
<u>.</u>
</kwd-group>
</root>
DESIRED-XML:
<?xml version="1.0" encoding="UTF-8"?><root>
<kwd-group>
<title>Keywords</title>
<u>, </u>
<kwd>Grassland conservation</kwd>
<u>, </u>
<kwd>Habitat conversion</kwd>
<kwd><b>bold</b></kwd>
<u>, </u>
<kwd>Irrigated rice</kwd>
<kwd><it>bss</it></kwd>
<kwd><it>ggggbss</it></kwd>
<u>, </u>
<kwd>Tonle Sap</kwd>
<u>ddd</u>
<kwd>ZBustard</kwd>
<u>.</u>
</kwd-group>
</root>
XSLT:
<xsl:variable name="sortedKeywords" select="1#5#13-14#9-10-11#7#3"/>
<xsl:for-each select="text">
<xsl:choose>
<xsl:when test="kwd and (../text/u or ../text/st)">
<xsl:variable name="pos" select="replace($sortedKeywords,'^([0-9]+)#.*?$','$1')"/>
***** Line 6 ****** <xsl:variable name="sortedKeywords" select="replace($sortedKeywords,'^[0-9]+#(.*?)$','$2')"/>
<xsl:copy-of select="../text[position()=number($pos)]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Line 6 alters the variable 'sortedKeywords'. But its not really changing.
Upvotes: 0
Views: 168
Reputation: 1233
Since the variable cannot be made global, i used the below approach to sort which had the challenge of sorting tags whose values are not actually inside a single tag.
XSLT-SOLUTION::
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="kwd-group">
<xsl:copy>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="kwd[preceding-sibling::*[1][self::u]]">
<xsl:sort select="string(.)" />
</xsl:apply-templates>
<xsl:apply-templates select="u[last()]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="kwd[preceding-sibling::*[1][self::u]]">
<xsl:apply-templates select="preceding-sibling::*[1][self::u]"/>
<xsl:copy><xsl:apply-templates/></xsl:copy>
<xsl:apply-templates select="following-sibling::kwd[not(preceding-sibling::*[1][self::u]) and preceding-sibling::u[1][following-sibling::*[1][self::kwd]=current()]]"/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 163458
OK, I still don't really understand the logic of what you are trying to achieve, but I can certainly see what you are doing wrong; variables in a functional language like XSLT aren't mutable, and you can't treat a for-each instruction in the same way as a procedural loop.
Unless there's some other algorithm for achieving what you want to achieve, you need to use recursion: write a template that processes one "text" element, and then calls itself to process the rest of the "text" elements, passing the new value of sortedKeywords as a parameter.
Upvotes: 1