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

使用这个路径,我可以得到一个这样的节点:</p>

<?xml version="1.0" encoding="utf-8"?>
<ClinicalDocument xmlns="urn:hl7-org:v3">
<component>
    <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>
</ClinicalDocument>

为了得到如下节点:

<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>

我将路径更改为:

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

但是,我怎样才能通过[code="10164-2"]作为资格获得相同的结果呢?

编辑 2012-12-17

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

这个 xpath 运行良好,我可以得到我想要的节点。如果我使用我该怎么办

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

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

4

2 回答 2

1

只需添加and section/code/@code='10164-2'到您的谓词:

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

namespace-uri()='urn:hl7-org:v3'注意:如果component不在该命名空间中,您可能必须删除。您在“为了获得如下节点的示例: ”没有命名空间。

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

于 2012-12-14T17:01:20.347 回答
1

使用

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

基于 XSLT 的验证

<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>

当此转换应用于提供的 XML 文档时:

<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 表达式求值,并将该求值的结果复制到输出中:

<component xmlns="urn:hl7-org:v3">
   <section>
      <code code="10164-2" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC"/>
      <title>History of Present Illness</title>
      <text/>
   </section>
</component>
于 2012-12-14T19:13:51.777 回答