排序的 exsl:节点集。按位置返回节点

发布于 2024-08-26 09:16:59 字数 844 浏览 2 评论 0原文

我有一组节点,

<menuList>
  <mode name="aasdf"/>
  <mode name="vfssdd"/>
  <mode name="aswer"/>
  <mode name="ddffe"/>
  <mode name="ffrthjhj"/>
  <mode name="dfdf"/>
  <mode name="vbdg"/>
  <mode name="wewer"/>
  <mode name="mkiiu"/>
  <mode name="yhtyh"/>
  and so on...
</menuList>

现在以这种方式对其进行排序

 <xsl:variable name="rtf">
    <xsl:for-each select="//menuList/mode">
       <xsl:sort data-type="text" order="ascending" select="@name"/>
          <xsl:value-of select="@name"/>
    </xsl:for-each>
 </xsl:variable>

现在我需要将排序数组中的任意元素获取到其位置数。我正在使用代码:

<xsl:value-of select="exsl:node-set($rtf)[position() = 3]"/>

并且收到响应错误。我应该怎样做呢?

I have a set of nodes

<menuList>
  <mode name="aasdf"/>
  <mode name="vfssdd"/>
  <mode name="aswer"/>
  <mode name="ddffe"/>
  <mode name="ffrthjhj"/>
  <mode name="dfdf"/>
  <mode name="vbdg"/>
  <mode name="wewer"/>
  <mode name="mkiiu"/>
  <mode name="yhtyh"/>
  and so on...
</menuList>

I have it sorted now this way

 <xsl:variable name="rtf">
    <xsl:for-each select="//menuList/mode">
       <xsl:sort data-type="text" order="ascending" select="@name"/>
          <xsl:value-of select="@name"/>
    </xsl:for-each>
 </xsl:variable>

Now I need to get an arbitrary element in the sorted array to the number of its position. I'm using the code:

<xsl:value-of select="exsl:node-set($rtf)[position() = 3]"/>

and I get a response error. How should I be doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜访吸血鬼 2024-09-02 09:17:00

提供的代码中至少有两个错误:

当更多时如果存在多个相邻文本节点,则将它们合并为一个。结果是 RTF 只有一个(长)单个文本节点,并且没有第三个节点。

2.< /strong>

这要求包含在 exsl:node-set($rtf) 中的第三个节点,但是 exsl:node-set($rtf) 是由 exsl:node-set() 扩展函数生成的临时树 - 这只是一个节点。因此,上面的 XPath 表达式根本不选择任何内容。

一个正确的解决方案如下

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 >

 <xsl:template match="/">
    <xsl:variable name="rtf">
        <xsl:for-each select="//menuList/mode">
           <xsl:sort data-type="text" order="ascending" select="@name"/>
              <xsl:copy-of select="."/>
        </xsl:for-each>
     </xsl:variable>
    <xsl:value-of select="msxsl:node-set($rtf)/*[position()=3]/@name"/>
 </xsl:template>
</xsl:stylesheet>

There are at least two errors in the provided code:

  1. <xsl:value-of select="@name"/>

When more than one adjacent text node exist, they are combined into one. The result is that the RTF has just one (long) single text node, and there isn't a 3rd node.

2.<xsl:value-of select="exsl:node-set($rtf)[position() = 3]"/>

This asks for the third node contained in exsl:node-set($rtf), however exsl:node-set($rtf) is the document node of the temporary tree produced by the exsl:node-set() extension function -- this is only one node. Therefore the above XPath expression doesn't select anything at all.

One correct solution is the following:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 >

 <xsl:template match="/">
    <xsl:variable name="rtf">
        <xsl:for-each select="//menuList/mode">
           <xsl:sort data-type="text" order="ascending" select="@name"/>
              <xsl:copy-of select="."/>
        </xsl:for-each>
     </xsl:variable>
    <xsl:value-of select="msxsl:node-set($rtf)/*[position()=3]/@name"/>
 </xsl:template>
</xsl:stylesheet>
你与清晨阳光 2024-09-02 09:17:00

您在变量中使用 。这不会复制节点,而是复制其字符串值(节点的@name属性值)。这意味着您会生成一个包含连接字符串的变量,但不包含任何结构。

尝试:

<xsl:variable name="rtf">
  <xsl:for-each select="//menuList/mode">
    <xsl:sort select="@name" data-type="text" order="ascending" />
    <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:variable>

现在您的变量包含一个结果树片段,该片段由按您首选顺序排列的 节点组成,这意味着 this:

<xsl:value-of select="exsl:node-set($rtf)/*[3]/@name" />
<!-- don't forget you have element nodes now! ^^^^^^ -->

可以工作。另请注意,…/*[position() = 3]…/*[3] 是同一件事。

You use <xsl:value-of> in your variable. This does not copy the node, but its string value (the node's @name attribute value). This means you produce a variable containing a concatenated string, but nothing with a structure.

Try:

<xsl:variable name="rtf">
  <xsl:for-each select="//menuList/mode">
    <xsl:sort select="@name" data-type="text" order="ascending" />
    <xsl:copy-of select="."/>
  </xsl:for-each>
</xsl:variable>

Now your variable contains a result tree fragment consisting of <mode> nodes in your preferred order, which means that this:

<xsl:value-of select="exsl:node-set($rtf)/*[3]/@name" />
<!-- don't forget you have element nodes now! ^^^^^^ -->

would work. Also note that …/*[position() = 3] and …/*[3] are the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文