Re: XSL transformation with Ant: sorting a list
"Scot P. Floess" <[email protected]>
| Newsgroups | gmane.comp.jakarta.ant.user |
|---|---|
| Message-ID | <alpine.DEB.2.00.1603070644590.22046@adminserver> |
So a few things - first you are killing yourself in the way you XSLT is
written. Personally, I'd do:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="/">
<Root>
<xsl:apply-templates select="a">
<xsl:sort select="a"/>
</xsl:apply-templates>
</Root>
</xsl:template>
<xsl:template name="a">
<result>
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</result>
</xsl:template>
</xsl:stylesheet>
The sort above may be wrong as I didn't test it - but you get the idea.
You are trying to do the work of the templating engine in your XSLT :)
See if this works and holler at me if you still can't get it working from
Ant...
On Mon, 7 Mar 2016, Al Le wrote:
> Hello,
>
> I'm facing a weird problem (IMHO) which, in short, is:
>
> a node list passed as a parameter to a template can't be sorted.
>
> The problem occurs only if I do the transformation from within Ant (I use Ant 1.8.4). It does not occur if I perform it via a Java program or in XmlSpy or through http://xsltransform.net.
>
> Here's an example.
>
> The input file (a.xml) is:
>
> <root>
> <a>BBB</a>
> <a>AAA</a>
> </root>
>
> The transformation (trans.xml) is:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:output method="xml" />
>
> <xsl:template match="/">
> <Root>
> <xsl:call-template name="processThis">
> <xsl:with-param name="nodeList" select="/*/a"/>
> </xsl:call-template>
> </Root>
> </xsl:template>
>
> <xsl:template name="processThis">
> <xsl:param name="nodeList" />
>
> <xsl:for-each select="$nodeList">
> <xsl:sort /><!-- ***** This causes the problem -->
> <xsl:variable name="thisVal" select="." />
> <result value="{$thisVal}" />
> </xsl:for-each>
> </xsl:template>
>
> </xsl:stylesheet>
>
> The Ant script is:
>
> <project default="trans">
>
> <target name="trans">
> <delete file="out.xml"/>
> <xslt style="trans.xsl" in="a.xml" out="out.xml"/>
> </target>
>
> </project>
>
>
> The expected output is:
>
> <Root>
> <result value="AAA" />
> <result value="BBB" />
> </Root>
>
> But if I run the ant script, just an empty 'Root' element is generated, without child nodes.
>
> If I comment out the line marked with '*****', the generated Root element does contain child elements, but unsorted (as one would expect in this case).
>
> If I process the elements directly (and not in a template passing the list to process as a parameter), then I can use 'sort', and everything works as expected. Here is the 'direct' transformation:
>
> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:output method="xml" />
>
> <xsl:template match="/">
> <Root>
> <xsl:for-each select="/*/a">
> <xsl:sort />
> <xsl:variable name="thisVal" select="." />
> <result value="{$thisVal}" />
> </xsl:for-each>
> </Root>
> </xsl:template>
>
> </xsl:stylesheet>
>
> So my question is: why isn't a list passed as a parameter to a template processed with a 'sort' option -- in Ant?
>
> Thank you!
> AL
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
Scot P. Floess RHCT (Certificate Number 605010084735240)
Chief Architect FlossWare http://sourceforge.net/projects/flossware
http://flossware.sourceforge.net
https://github.com/organizations/FlossWare