xpath / xslt确定相对于所有同名节点的上下文节点的索引?

时间:2010-03-26 22:39:34

标签: xml xslt xpath

给定下面的模式 - 使用“查询”节点作为上下文,是否可以使用xpath / xslt确定相对于文档中所有“查询”节点的“查询”节点的索引(而不是position()相对于它的直接兄弟姐妹)。

TIA,

杰夫

<section>
    <name>About</name>
    <link>about</link>
    <questions>
        <question>
            <query>Question 1</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
    </questions>
</section>

<section>
    <name>Contact</name>
    <link>contact</link>
    <questions>
        <question>
            <query>Question 1</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
        <question>
            <query>Question 2</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
    </questions>
</section>

1 个答案:

答案 0 :(得分:7)

使用

count(preceding::question)+1

此转换会执行详尽的测试

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vQs" select="//question"/>

 <xsl:template match="/">
  <xsl:for-each select="$vQs">
    <xsl:value-of select=
    "count(preceding::question)+1"/>
    <xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

将以上转换应用于以下XML文档

<t>
    <section>
        <name>About</name>
        <link>about</link>
        <questions>
            <question>
                <query>Question 1</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 2</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
        </questions>
    </section>
    <section>
        <name>Contact</name>
        <link>contact</link>
        <questions>
            <question>
                <query>Question 3</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 4</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 5</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
        </questions>
    </section>
</t>

产生了想要的结果:

1 2 3 4 5