选择所需元素的一个 XPath 表达式是:
/b:ViewItem
/b:Fields
/c:KeyValueOfstringanyType
[c:Key = 'ID']
/c:Value
Do note所提供的 XML 文档具有命名空间,并且任何包含无前缀元素名称的 XPath 表达式都不会选择所需的元素,但以下形式的表达式除外:
/*[local-name() = 'ViewItem']
/*[local-name() = 'Fields']
/*[local-name() = 'KeyValueOfstringanyType']
[*[local-name() = 'Key'] = 'ID']
/*[local-name() = 'Value']
另外,对于上面的第一个 XPath 表达式,命名空间前缀为"b:"
and "c:"
必须“注册”(请阅读 XPath 引擎的文档,了解如何执行此操作)。
基于XSLT的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="some:b" xmlns:c="some:c">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/b:ViewItem
/b:Fields
/c:KeyValueOfstringanyType
[c:Key = 'ID']
/c:Value
"/>
==============
<xsl:copy-of select=
"/*[local-name() = 'ViewItem']
/*[local-name() = 'Fields']
/*[local-name() = 'KeyValueOfstringanyType']
[*[local-name() = 'Key'] = 'ID']
/*[local-name() = 'Value']
"/>
</xsl:template>
</xsl:stylesheet>
当此 XSLT 转换应用于以下 XML 文档时(所提供的一项,已针对严重畸形进行了纠正):
<b:ViewItem xmlns:b="some:b" xmlns:c="some:c">
<b:Fields>
<c:KeyValueOfstringanyType>
<c:Key>ID</c:Key>
<c:Value>1900310 </c:Value>
</c:KeyValueOfstringanyType>
<c:KeyValueOfstringanyType>
<c:Key>SubType</c:Key>
<c:Value>U003_O100_00IT_609644</c:Value>
</c:KeyValueOfstringanyType>
<c:KeyValueOfstringanyType>
<c:Key>SectionType</c:Key>
<c:Value>Released</c:Value>
</c:KeyValueOfstringanyType>
</b:Fields>
</b:ViewItem>
计算两个 XPath 表达式并输出它们选择的节点:
<c:Value xmlns:c="some:c" xmlns:b="some:b">1900310 </c:Value>
==============
<c:Value xmlns:c="some:c" xmlns:b="some:b">1900310 </c:Value>