xml - 如何使用xslt2.0检索当前节点的(ancestor::w:p)的先前同级?

标签 xml xslt linq-to-xml

这是我的xml文件。

<w:document>
    <w:body>
        <w:p>
            <w:pPr>
                  <w:pStyle w:val="Heading1"/>
            </w:pPr>
            <w:r>
               <w:t>Para1</w:t>
            </w:r>
        </w:p>

       <w:p>
        <w:pPr>
           <w:pStyle w:val="Heading2"/>
        </w:pPr>
        <w:r>
          <w:t>Para2</w:t>
        </w:r>
        </w:p>

        <w:p>   <!-- Current Node -->
          <w:pPr>
            <w:pStyle w:val="Heading3"/>   
          </w:pPr>
          <w:r>
              <w:t>Para3</w:t>
          </w:r>
         </w:p>
    </w:body>
</w:document>

所以,


  • 现在,我想获取当前节点的祖先<w:p>
  • ,那么我需要获取此<w:p> [1]的在前同级文件
  • 最后需要检索<w:pPr>-<w:pStyle> [1]的<w:p>-> @ w:val。


  • 我尝试过这个...但是失败了...
    preceding-sibling::(ancestor::w:p)[1]/w:pPr/w:pStyle/@w:val
    

    最佳答案

    如果我理解正确,则您需要:

    ancestor::w:p[1]/preceding-sibling::w:p[1]/w:pPr/w:pStyle/@w:val
    

    如果您当前的节点是<w:pStyle w:val="Heading3"/>

    如果当前节点是w:p,则您不再需要祖先,而是直接在前一个同级:
    preceding-sibling::w:p[1]/w:pPr/w:pStyle/@w:val
    

    希望能帮助到你。

    关于xml - 如何使用xslt2.0检索当前节点的(ancestor::w:p)的先前同级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7440699/

    相关文章:

    c# - 如何创建具有特定命名空间的 XElement?

    c# - Linq to Xml,保持 XDocument 加载?

    c# - 检查名称属性中是否存在值

    java - ConstraintLayout 中奇怪的 TextView 行为

    java - 如何编写格式正确的 xml

    java标准库产生错误的xml 1.1

    xslt - 在 XSLT 1.0 中创建 XPath 函数

    xml - 选择元素之间的多个 text() 节点

    c# - 如何在 C# 中停止 IIS 7 锁定 .XSLT 文件

    sql-server - 在 SQL 中优化 XML