1

I need to sort some content but only when an attribute is equal to CAT. I think I should be able to pass a property from my ant build file to the use-when attribute but it is not working. Any help would be appreciated

Here is the xslt that I have:

<xsl:for-each select="document(@fileRef)/foo/bar">
    <xsl:sort select="translate(child::Title/text(), '&gt;', '')" order="ascending" use-when="system-property('customerCode')='CAT'"
                          collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"/>
<!-- do some stuff here -->
</xsl:for-each>
  • Yes, this should be possible in principle. But I'm not sure of the detail of how to set Java system properties from Ant. – Michael Kay Jun 08 '21 at 18:11
  • @MichaelKay is this the only way to achieve this? https://stackoverflow.com/questions/16695538/xslsort-does-not-work-together-with-xslchoose-or-if – user3618078 Jun 08 '21 at 21:25
  • I would try to first establish whether your `system-property('customerCode')` code comes through as you expect it. The conditional `use-when` on an `xsl:sort` works fine for me at e.g. https://xsltfiddle.liberty-development.net/eieFzZZ/0, https://xsltfiddle.liberty-development.net/eieFzZZ/1, https://xsltfiddle.liberty-development.net/eieFzZZ/2, https://xsltfiddle.liberty-development.net/eieFzZZ/3. Perhaps show your Ant code and add a tag for Ant to see whether someone can tell more about the right way to get the value from a system property into the XSLT using Ant. – Martin Honnen Jun 08 '21 at 21:50
  • You could also do it using static parameters; the problem is that Ant's xslt task hasn't been upgraded to recognise such concepts. – Michael Kay Jun 09 '21 at 07:34

1 Answers1

1

Using oXygen I got the following to work in an Ant file:

<xslt in="sample1.xml" out="sample1-transformed.xml" force="true" style="sheet1.xsl">
    <factory name="net.sf.saxon.TransformerFactoryImpl"/>
    <sysproperty key="cat" value="bar"/>
</xslt>

XML sample1.xml is e.g.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item>c</item>
    <item>a</item>
</root>

XSLT is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
        <xsl:comment select="system-property('cat')"/>
        <xsl:next-match/>
    </xsl:template>
    
    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="item">
                <xsl:sort select="." use-when="system-property('cat') = 'foo'"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

and then the output items are sorted only if the Ant sets e.g. <sysproperty key="cat" value="foo"/>.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110