【问题标题】:How to read specific elements from xml data?如何从 xml 数据中读取特定元素?
【发布时间】:2015-08-05 21:32:50
【问题描述】:

以下是我的回复数据:

<Bundle xmlns="http://hl7.org/fhir">
   <id value="ffd821ee-f4d0-43fc-8eb1-df1d9bd63340"/>
   <meta>
      <lastUpdated value="2015-08-05T09:14:57.242-04:00"/>
   </meta>
   <type value="searchset"/>
   <base value="http://fhirtest.uhn.ca/baseDstu2"/>
   <total value="535"/>
   <link>
      <relation value="self"/>
      <url value="http://fhirtest.uhn.ca/baseDstu2/Patient?_format=xml"/>
   </link>
   <link>
      <relation value="next"/>
      <url value="http://fhirtest.uhn.ca/baseDstu2?_getpages=0bad92c1-cfe3-4a23-bd20-e24e854c16da&amp;_getpagesoffset=10&amp;_count=10&amp;_format=xml&amp;_pretty=true"/>
   </link>
</Bundle>

我需要读取&lt;link&gt;&lt;url&gt; 标签的value 属性,其中&lt;relation&gt;value 属性是next

所以,我的预期输出是http://fhirtest.uhn.ca/baseDstu2?_getpages=0bad92c1-cfe3-4a23-bd20-e24e854c16da&amp;amp;_getpagesoffset=10&amp;amp;_count=10&amp;amp;_format=xml&amp;amp;_pretty=true

读取此内容的 XPath 表达式是什么?

【问题讨论】:

  • 基于the previous question,您可能正在寻找这种XPath:/fhi:Bundle/fhi:link[fhi:relation/@value = 'next']/fhi:url/@value,正如下面的答案中提到的那样

标签: xml xslt xpath


【解决方案1】:

答案取决于您用来评估 XPath 表达式的工具或库。如果在最外层元素上声明的默认命名空间自动对 XPath 引擎可用或被忽略,则将执行以下操作:

string(/Bundle/link[relation/@value = 'next']/url/@value)

如果不是这种情况,您需要将此命名空间 URI 与前缀(特定于工具或库)一起注册,然后在 XPath 表达式中使用前缀:

string(/fhi:Bundle/fhi:link[fhi:relation/@value = 'next']/fhi:url/@value)

既然您用 XSLT 标记了这个问题,也许您在 XSLT 中使用 XPath?然后在xsl:stylesheet 元素上声明这个命名空间:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fhi="http://hl7.org/fhir">

或者,要完全忽略命名空间(不推荐),请使用

string(/*[local-name() = 'Bundle']/*[local-name() = 'link'][*[local-name() = 'relation']/@value = 'next']/*[local-name() = 'url']/@value)

在所有情况下,结果都是

http://fhirtest.uhn.ca/baseDstu2?_getpages=0bad92c1-cfe3-4a23-bd20-e24e854c16da&_getpagesoffset=10&_count=10&_format=xml&_pretty=true

【讨论】:

    猜你喜欢
    • 2021-03-30
    • 2019-12-28
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多