且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从xml数据中读取特定元素?

更新时间:2023-11-13 18:36:46

答案取决于您用来计算 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