Re: [xsl] Detecting precedence of string content in XSLT 1.0

Subject: Re: [xsl] Detecting precedence of string content in XSLT 1.0
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 21 Dec 2006 09:14:18 +0100
Hi Harry,

Run the following example on itself and you will get the output you desire. If you have trouble adopting it for your needs, please tell so.

Note that you may want to test for empty strings, when tokenizing results in such (the output of below will include two empty results, one for the first newline and one for the closing newline). You can do so by adding the following statement to the appropriate places:

normalize-space(substring-after($string, '&#xA;')) != ''

Cheers,
-- Abel

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:variable name="input">
fw_rule3
fw_rule1
fw_rule2
</xsl:variable>
<xsl:template match="/">
<xsl:call-template name="stringtok">
<xsl:with-param name="string" select="$input"/>
<xsl:with-param name="pos" select="0" />
</xsl:call-template>
</xsl:template>
<xsl:template name="stringtok">
<xsl:param name="string" />
<xsl:param name="pos" />
<rule name="{normalize-space(substring-before($string, '&#xA;'))}" precedence="{$pos}" />
<xsl:if test="contains($string, '&#xA;')">
<xsl:call-template name="stringtok">
<xsl:with-param name="string" select="substring-after($string, '&#xA;')"/>
<xsl:with-param name="pos" select="$pos + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


Harry Liljestrvm wrote:
Hi,

I have following input:
<fw-rule-order>
  fw_rule3
  fw_rule1
  fw_rule2
</fw-rule-order>

and the desired output:
<rule name="fw_rule3" precedence="0"/>
<rule name="fw_rule1" precedence="1"/>
<rule name="fw_rule2" precedence="2"/>

Could someone give me any hints how this could be solved in XSLT 1.0?

Current Thread