【问题标题】:Is there a way to conditionally sort in xslt?有没有办法在 xslt 中有条件地排序?
【发布时间】:2021-06-08 17:37:17
【问题描述】:

我需要对一些内容进行排序,但前提是属性等于 CAT。我想我应该能够将我的 ant 构建文件中的属性传递给 use-when 属性,但它不起作用。任何帮助将不胜感激

这是我拥有的 xslt:

<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>

【问题讨论】:

标签: xpath saxon xslt-3.0


【解决方案1】:

使用 oXygen,我可以在 Ant 文件中执行以下操作:

<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 是例如

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

XSLT 是

<?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>

然后输出items 仅在 Ant 设置时排序,例如&lt;sysproperty key="cat" value="foo"/&gt;.

【讨论】:

  • 我没有在我的 ant 构建文件的 元素中添加 。现在像魅力一样工作
猜你喜欢
  • 2019-06-27
  • 1970-01-01
  • 2012-12-06
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
相关资源
最近更新 更多