如何从xml数据中读取特定元素? [英] How to read specific elements from xml data?

查看:27
本文介绍了如何从xml数据中读取特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的回复数据:

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

我需要读取 标签的 value 属性,其中 value 属性;relation>next.

I need to read value attribute of that <link><url> tag in which value attribute of <relation> is next.

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

So, my expected output is http://fhirtest.uhn.ca/baseDstu2?_getpages=0bad92c1-cfe3-4a23-bd20-e24e854c16da&amp;_getpagesoffset=10&amp;_count=10&amp;_format=xml&amp;_pretty=true.

阅读本文的 XPath 表达式是什么?

What will be the XPath expression for reading this?

推荐答案

答案取决于您用来计算 XPath 表达式的工具或库.如果在最外层元素上声明的默认名称空间自动提供给 XPath 引擎或被忽略,则将执行以下操作:

The answer depends on the tool or library you use to evaluate XPath expressions. If default namespaces declared on the outermost element are automatically made available to the XPath engine or ignored, the following will do:

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

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

If this is not the case you need to register this namespace URI together with a prefix (specific to the tool or library) and then use the prefix in the XPath expression:

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

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

Since you tagged this question with XSLT, perhaps you are using XPath within XSLT? Then declare this namespace on the xsl:stylesheet element:

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

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

Alternatively, to ignore namespaces alltogether (not recommended), use

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

在所有情况下,结果都是

In all cases, the result will be

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

这篇关于如何从xml数据中读取特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆