【问题标题】:using preceding-sibling with with xsl:sort使用带有 xsl:sort 的前置兄弟
【发布时间】:2017-03-15 09:48:19
【问题描述】:

我正在尝试将前同级和后同级与记录子集一起使用,并对它们进行排序。前/后带回原始xml顺序值的问题:

<Salaries>
    <Salary>
        <Base>1000</Base>
        <CreatedDate xmlns:d7p1="http://schemas.datacontract.org/2004/07/System">
            <d7p1:DateTime>2016-01-09T14:38:54.8440764Z</d7p1:DateTime>
            <d7p1:OffsetMinutes>0</d7p1:OffsetMinutes>
        </CreatedDate>
    </Salary>
    <Salary>
        <Base>2000</Base>
        <CreatedDate xmlns:d7p1="http://schemas.datacontract.org/2004/07/System">
            <d7p1:DateTime>2015-01-09T14:38:54.8440764Z</d7p1:DateTime>
            <d7p1:OffsetMinutes>0</d7p1:OffsetMinutes>
        </CreatedDate>
    </Salary>
    <Salary>
        <Base>3000</Base>
        <CreatedDate xmlns:d7p1="http://schemas.datacontract.org/2004/07/System">
            <d7p1:DateTime>2017-01-09T14:38:54.8440764Z</d7p1:DateTime>
            <d7p1:OffsetMinutes>0</d7p1:OffsetMinutes>
        </CreatedDate>
    </Salary>
</Salaries>

例如,当我在 for-each (Salaries/Salary) 下使用排序和 c# 函数将偏移分钟添加到日期中并转换为长数字 201701010000 时(以使 xslt 中的操作更容易)。

<xsl:sort select="number(cs:Convertdatetolong(cs:AddOffsetMinutes(substring(p:CreatedDate/d5p1:DateTime,1,19),p:CreatedDate/d5p1:OffsetMinutes)))" order="ascending"/>

排序完美,我按以下顺序取出记录:

  • 2000
  • 1000
  • 3000

如果我使用前面的兄弟/前面(和后面),就会出现问题。我希望第一条记录(2000)没有前面的记录,最后一条记录(3000)没有后面的记录。 但是,当我使用前面/后面时,我会从原始 XML 中获取上一条记录和下一条记录:

  • 2000(前 - 1000 / 后 - 3000)
  • 1000(前 - / 后 - 2000)
  • 3000(之前 - 2000 / 之后 - )

我希望能够与上一条记录(按排序顺序)和当前记录(按排序顺序)进行比较:

  • 2000(前 - / 后 - 1000)
  • 1000(前 - 2000 / 后 3000)
  • 3000(前 - 1000 / 后 - )

我已经尝试过前置兄弟和前置

<xsl:value-of select="preceding::p:Salary[1]/p:Base"/>
<xsl:value-of select="preceding-sibling::p:Salary[1]/p:Base"/>
<xsl:value-of select="preceding::p:Salary[position()=1]/p:Base"/>

(工资在不同的命名空间(p) 这真的可能吗,还是我必须使用变量来保存以前记录的数据以进行比较?

感谢您的任何想法。我正在使用 xslt 1.0

【问题讨论】:

    标签: sorting xslt


    【解决方案1】:

    虽然 XSLT/XPath 经常谈论“节点序列”,但实际上将其视为“节点引用序列”更准确 - 因为,例如,同一个节点可以在序列中出现多次.当您对一系列节点引用进行排序时,您不会以任何方式更改单个节点,您只会更改顺序。这意味着节点仍然存在于它们原来所在的树中,它们的父母、兄弟姐妹和后代都和以前一样。

    你想要的不是节点的前后兄弟节点,而是按排序顺序出现在它之前和之后的节点,这是完全不同的东西。

    这样做的一种方法是构建一个包含原始节点副本的新树,例如,如果你这样做了,你会得到它

    <xsl:variable name="x">
      <xsl:for-each ...>
        <xsl:sort ...>
          <xsl:copy-of select="."/>
    

    复制节点的兄弟关系将反映排序顺序。在 XSLT 1.0 中存在一个小问题,$x 是一个结果树片段,因此您必须使用 exslt:node-set() 函数将其转换为节点集。

    事实上,在 XSLT 1.0 中,这可能是唯一的方法,因为 XSLT 1.0 数据模型只有节点集,没有序列,这意味着除了文档之外没有其他方法可以捕获和处理节点序列命令。 2.0 模型具有更大的灵活性和功能。如果可以,请升级 - XSLT 1.0 已接近 20 年。

    【讨论】:

    • 谢谢。我已经设法按照我想要的顺序将值和日期放入节点集中。我正在努力在 for-each 中循环它们: 似乎不起作用
    • 啊,我不得不使用相同的命名空间。多哈。 msxsl:node-set($SAL)//p:薪水
    【解决方案2】:

    感谢迈克尔的回答。为了完整起见,在此处发布。由于 xml 中使用的名称空间而变得复杂:

      <!-- Puts the whole of the Salary Node into a variable-->
      <xsl:variable name="SALARY" >
        <xsl:copy-of select="p:Salaries" />
      </xsl:variable>
    
      <!-- Puts the the required key data into a node-set with the correct sort applied-->
      <xsl:variable name="SAL">
        <xsl:for-each select="msxsl:node-set($SALARY)//p:Salary">
          <xsl:sort select="number(cs:Convertdatetolong(cs:AddOffsetMinutes(substring(p:CreatedDate/d5p1:DateTime,1,19),p:CreatedDate/d5p1:OffsetMinutes)))" order="ascending"/>
          <xsl:copy-of select="." />
        </xsl:for-each>
      </xsl:variable>
    
    <!-- Quick Output-->
      <xsl:for-each select="msxsl:node-set($SAL)//p:Salary">
        <xsl:text>Sa:</xsl:text>
        <xsl:value-of select="position()" />
        <xsl:text>Preceding:</xsl:text>
        <xsl:value-of select="preceding-sibling::p:Salary[1]/p:Base"/>
        <xsl:value-of select="$newline" />
        <xsl:text>Current:</xsl:text>
        <xsl:value-of select="p:Base"/>
        <xsl:value-of select="$newline" />
        <xsl:text>Following:</xsl:text>
        <xsl:value-of select="following-sibling::p:Salary[1]/p:Base"/>
        <xsl:value-of select="$newline"/>
      </xsl:for-each>
    

    【讨论】:

      【解决方案3】:

      preceding-sibling 轴按文档顺序获取上下文节点的前面兄弟姐妹。

      要在排序后引用节点的前面的兄弟节点,您需要首先将排序后的节点存储在一个变量中 - 并且在 XSLT 1.0 中,将该变量转换为一个节点集。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 2019-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多