Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xpath 如何获取父节点?_Xpath - Fatal编程技术网

Xpath 如何获取父节点?

Xpath 如何获取父节点?,xpath,Xpath,使用此路径,我可以获得如下节点: //* [ local-name()='component' and namespace-uri()='urn:hl7-org:v3' ] 但是,如何使用[code=“10164-2”]作为限定条件来获得相同的结果 编辑2012-12-17 //:component[1]//:component[.//:section/:code[@code='10164-2']] 这个xpath工作得很好,我可以得到我想要的节点。如果我使用 //*[local-nam

使用此路径,我可以获得如下节点:

//* [ local-name()='component' and namespace-uri()='urn:hl7-org:v3'   ] 
但是,如何使用
[code=“10164-2”]
作为限定条件来获得相同的结果

编辑2012-12-17

//:component[1]//:component[.//:section/:code[@code='10164-2']]

这个xpath工作得很好,我可以得到我想要的节点。如果我使用

//*[local-name()='component'和namespace-uri()='urn:hl7 org:v3'])[1]


要选择父节点,请将[@code='10164-2']添加到谓词部分,以获得所需的子节点。(我不想在路径中使用:以避免名称空间问题)

只需在谓词中添加
和/code/@code='10164-2'

//* [ local-name()='component' and namespace-uri()='urn:hl7-org:v3'  and  position()=1] 
注意:如果
组件
不在该名称空间中,您可能必须删除
名称空间-uri()='urn:hl7 org:v3'
。“为了获得如下所示的节点:”中的示例没有名称空间

另外,我使用了
*:component
而不是
local-name()
,因为您将问题标记为XPath 2.0。

使用

//*:component[namespace-uri()='urn:hl7-org:v3' and section/code/@code='10164-2']
      ((//*[local-name()='component'
         and namespace-uri()='urn:hl7-org:v3']
       )[1]
          //*[local-name()='component'
            and
              namespace-uri()='urn:hl7-org:v3'
             ]
      )[1]
基于XSLT的验证

//*:component[namespace-uri()='urn:hl7-org:v3' and section/code/@code='10164-2']
      ((//*[local-name()='component'
         and namespace-uri()='urn:hl7-org:v3']
       )[1]
          //*[local-name()='component'
            and
              namespace-uri()='urn:hl7-org:v3'
             ]
      )[1]

在提供的XML文档上应用此转换时:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "((//*[local-name()='component'
         and namespace-uri()='urn:hl7-org:v3']
     )[1]
        //*[local-name()='component'
            and
              namespace-uri()='urn:hl7-org:v3']
     )[1]
     "/>
 </xsl:template>
</xsl:stylesheet>
<component xmlns="urn:hl7-org:v3">
    <structuredBody>
      <component>
          <section>
          <code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
          <title>History of Present Illness</title>
          <text>
          </text>
          </section>
      </component>
    <component>     ......      </component>
    <component>     ......      </component>
</structuredBody>
</component>

计算XPath表达式,并将计算结果复制到输出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "((//*[local-name()='component'
         and namespace-uri()='urn:hl7-org:v3']
     )[1]
        //*[local-name()='component'
            and
              namespace-uri()='urn:hl7-org:v3']
     )[1]
     "/>
 </xsl:template>
</xsl:stylesheet>
<component xmlns="urn:hl7-org:v3">
    <structuredBody>
      <component>
          <section>
          <code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
          <title>History of Present Illness</title>
          <text>
          </text>
          </section>
      </component>
    <component>     ......      </component>
    <component>     ......      </component>
</structuredBody>
</component>

Dimitre novatchev。谢谢你的回答。我明天可以看吗?因为今天我得先完成报告并把它寄给我的教授。我会尝试使用你的解决方案,然后回复你。好的,我会记住的。丹尼尔·海利。谢谢你的帮助。我会尝试使用你的解决方案,然后回复你。此xml文档的默认命名空间为urn:hl7 org:v3。//*:组件[namespace-uri()='urn:hl7 org:v3'和section/code/@code='10164-2']我试图使用它,但[javax.xml.transform.transformerexception额外非法令牌]的异常被抛出。然后我将路径更改为/*[local-name()='component'和namespace-uri()='urn:hl7 org:v3'和/code/@code='10164-2'],但没有返回任何节点。@chris.shi,为了让您收到任何有用的答案,您需要向我们显示XML文档——完整,但尽可能小,根据其计算XPath表达式,并精确指定要选择的节点。如果没有这些重要信息,人们就无法重现您的问题,他们提供给您的任何XPath表达式在对他们从未见过的XML文档进行评估时都可能无法选择想要的节点。@chris.shi,我已经更新了答案,并验证了新的XPath表达式选择了您想要的
组件
元素。请试一试。谢谢你的大力帮助。我还更新了xml文档以完成结构。