Xml 这是否可以计算属于<;子节点的当前节点的索引位置;w:车身>;使用xslt2.0?

Xml 这是否可以计算属于<;子节点的当前节点的索引位置;w:车身>;使用xslt2.0?,xml,xslt,xpath,Xml,Xslt,Xpath,这是我的Xml文档 <w:document xmlns:w="w"> <w:body> <w:p> <w:r> <w:t> Para1 </w:t> </w:r> </w:p> <w:p> <w:r> <w

这是我的Xml文档

<w:document xmlns:w="w">
<w:body>
   <w:p>
        <w:r>
           <w:t>
               Para1
            </w:t>
        </w:r>
     </w:p>
   <w:p>
        <w:r>
           <w:t>
               Para2
            </w:t>
        </w:r>
     </w:p>
   <w:p>
        <w:r>
           <w:t>
               Para3
            </w:t>
        </w:r>
     </w:p>
   <w:p>
        <w:r>
           <w:t>
               Para4
            </w:t>
        </w:r>
     </w:p>
   <w:p>
        <w:r>
           <w:t>
               Para5
            </w:t>
        </w:r>
     </w:p>

   <w:tbl>
         <w:tr>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para6
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para7 <!-- Just Assume, this is current Node -->
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
           </w:tr>
        </w:tbl>
     <w:p>
        <w:r>
           <w:t>
               Para8
            </w:t>
        </w:r>
     </w:p>

</w:body>
</w:document>

帕拉1
帕拉2
帕拉3
帕拉4
Para5
Para6
帕拉7
帕拉8
所以,现在我想得到属于
子节点的当前节点的索引位置。因此,我的预期输出是:6

比如说,

  • 如果当前节点是para6,那么我的输出也是6
  • 如果当前节点为para8,则我的输出为7
  • 如果当前节点为para5,则我的输出为5
  • 如果可能的话,请引导我得到这个

    新更新:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                                  xmlns:v="urn:schemas-microsoft-com:vml"
                                  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                                  xmlns:exsl="http://exslt.org/common"
                                  xmlns:fn="http://www.w3.org/2005/xpath-functions"
                                  extension-element-prefixes="exsl">
    
    
      <xsl:output method="html" indent="yes"/>
    
        <xsl:template match="*">
          <Document>
             <xsl:apply-templates select="//w:p">
             </xsl:apply-templates>
          </Document>
        </xsl:template>
    
      <xsl:template match="w:p">
    
           <xsl:variable name="index">
                <xsl:call-template name="get-para-index">
                     <xsl:with-param name="node" select="."/>
                </xsl:call-template>
           </xsl:variable>
    
         <Paragraph>
                <xsl:attribute name="index">
                     <xsl:value-of select="$index" />
                </xsl:attribute>
    
                <xsl:apply-templates select="./w:r/w:t"/>
         </Paragraph>
    
      </xsl:template>
    
      <xsl:template match="w:t">
        <xsl:value-of select="."/>
      </xsl:template>
    
      <xsl:template name="get-para-index">
           <xsl:param name="node"/>
        <xsl:value-of select="count($node/ancestor::*[parent::w:body]/preceding-sibling::*)+1"/>     <!-- Need to write logic here -->
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    
    我只是调用
    get para index模板
    ,以获取每个
    的索引位置。但它总是为每个
    返回1。请引导我走出这个问题

    谢谢和问候,
    P.SARAVANAN

    我不是100%确定你想要什么,但是这个表达

    count(ancestor::*[parent::w:body]/preceding-sibling::*)+1
    

    从上下文节点中,查找其父级为的祖先,然后选择所有前面的同级元素并对这些元素进行计数。添加1应该会给你这个职位。

    我更新的Xslt满足了我的要求

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                                  xmlns:v="urn:schemas-microsoft-com:vml"
                                  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
                                  xmlns:exsl="http://exslt.org/common"
                                  xmlns:fn="http://www.w3.org/2005/xpath-functions"
                                  extension-element-prefixes="exsl">
    
    
      <xsl:output method="html" indent="yes"/>
    
        <xsl:template match="*">
          <Document>
             <xsl:apply-templates select="//w:p">
             </xsl:apply-templates>
          </Document>
        </xsl:template>
    
      <xsl:template match="w:p">
    
           <xsl:variable name="index">
                <xsl:call-template name="get-para-index">
                     <xsl:with-param name="node" select="."/>
                </xsl:call-template>
           </xsl:variable>
    
         <Paragraph>
                <xsl:attribute name="index">
                     <xsl:value-of select="$index" />
                </xsl:attribute>
    
                <xsl:apply-templates select="./w:r/w:t"/>
         </Paragraph>
    
      </xsl:template>
    
      <xsl:template match="w:t">
        <xsl:value-of select="."/>
      </xsl:template>
    
      <xsl:template name="get-para-index">
           <xsl:param name="node"/>
    
        <xsl:choose>
          <xsl:when test="$node/ancestor::*[parent::w:body]">
            <xsl:value-of select="count($node/ancestor::*[parent::w:body]/preceding-sibling::*)"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="count($node/preceding-sibling::*)"/>
          </xsl:otherwise>
        </xsl:choose>          
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    
    您的输入XML无效。在接近尾端的地方似乎有一个游荡的w:p元素。请你把它迅速整理一下,以免引起混乱好吗?谢谢@_蒂姆:我对此感到非常抱歉。我一定会把有效的内容放得更远。请现在参考我的问题…@Tim:你检查过我更新的问题吗?…有人请帮我解决这个问题吗四十二的答案对我来说是正确的。这就是我在他第一个进去之前要给出的答案@蒂姆:例如,我将当前节点作为名为node的参数传递。然后,我想计算父节点为的$node的祖先,然后取该祖先的前一个兄弟节点+1。但他的根本不起作用。我认为您需要在这里使用祖先或自我,而不是祖先,因为否则,如果当前节点是w:p标记,而该标记是w:body标记的直接子标记,则该方法将不起作用。@TimC:非常感谢您的支持。我找到了一种方法。但我不知道这是否是最好的方法?。请告诉我你对我的解决方案的看法。我现在就把我的答案写出来。。。