【问题标题】:XSL associative sorting using a field substring使用字段子字符串的 XSL 关联排序
【发布时间】:2010-12-15 15:24:12
【问题描述】:

我正在编写的转换必须由给定节点集中的逗号分隔字符串值组成。生成的字符串必须根据输入值中第一个字符的随机(非字母)映射进行排序。

我想出了这个:

<?xml version="1.0" encoding="utf-8"?>    
<xsl:stylesheet    
       version="1.0"    
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    
       xmlns:tmp="http://tempuri.org"    
       exclude-result-prefixes="tmp"    
>    
       <xsl:output method="xml" indent="yes"/>

       <tmp:sorting-criterion>    
             <code value="A">5</code>    
             <code value="B">1</code>    
             <code value="C">3</code>    
       </tmp:sorting-criterion>

       <xsl:template match="/InputValueParentNode">    
             <xsl:element name="OutputValues">    
             <xsl:for-each select="InputValue">    
                    <xsl:sort select="document('')/*/tmp:sorting-criterion/code[@value=substring(.,1,1)]" data-type="number"/>    
                    <xsl:value-of select="normalize-space(.)"/>   
                    <xsl:if test="position() != last()">    
                           <xsl:text>,</xsl:text>    
                    </xsl:if>
             </xsl:for-each>    
             </xsl:element>    
       </xsl:template>    
</xsl:stylesheet>

它不起作用,看起来 XPath document('')/*/tmp:sorting-criterion/code[@value=substring(.,1,1)] 没有按我的预期进行评估。我已经检查过用 substring(.,1,1) 代替文字,它的计算结果是正确的值。

那么,我是否遗漏了一些使排序 XPath 表达式无法按预期进行评估的东西,或者以这种方式根本不可能做到这一点?

如果无法创建有效的 XPath 表达式,是否有解决方法来实现我的目的?

注意:我受限于 XSLT-1.0

示例输入:

<?xml version="1.0" encoding="utf-8"?>
<InputValueParentNode>
       <InputValue>A input value</InputValue>
       <InputValue>B input value</InputValue>
       <InputValue>C input value</InputValue>
</InputValueParentNode>

预期输出:

<?xml version="1.0" encoding="utf-8"?>
<OutputValues>B input value,C input value,A input value</OutputValues>

【问题讨论】:

  • 您能否提供示例输入和预期输出?
  • @wdebeaum:问题已编辑以包含两者。干杯。

标签: xslt


【解决方案1】:

self::node() 缩写. 替换为current() 函数。

更好的谓词是:starts-with(normalize-space(current()),@value)

【讨论】:

  • 使用 current() 是 Antonio 发布的表达式的正确修复方法。
  • @Alejandro:非常感谢,它有效!我将仔细研究当前/上下文节点的差异:)
  • @Antonio Perez:你很高兴。 current() 是一个 XSLT 函数,返回 XSLT 指令执行的上下文节点。 . 是 XPath 1.0 中 self::node() 的缩写,意思是上下文节点,但这会随着 XPath 表达式中的每一步而改变。
【解决方案2】:

除了根据Alejandro´s answer, 更改转换之外,我发现最好使用 XSL 变量来映射数据,以避免在Dimitre´s answer to another related question 中看到的虚拟命名空间 (tmp) 的声明。

我的最终实现:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
       version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> 
       <xsl:output method="xml" indent="yes"/>

       <xsl:template match="/InputValueParentNode">
             <xsl:variable name="sorting-map">
                    <i code="A" priority="5"/>
                    <i code="B" priority="1"/>
                    <i code="C" priority="3"/>
             </xsl:variable>
             <xsl:variable name="sorting-criterion" select="document('')//xsl:variable[@name='sorting-map']/*"/>

             <xsl:element name="OutputValues">
             <xsl:for-each select="InputValue">
                    <xsl:sort select="$sorting-criterion[@code=substring(normalize-space(current()),1,1)]/@priority" data-type="number"/>
                    <xsl:value-of select="normalize-space(current())"/>
                    <xsl:if test="position() != last()">
                           <xsl:text>,</xsl:text>
                    </xsl:if>
             </xsl:for-each>
             </xsl:element>
       </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多