我想按照 w3schools 中的这个示例使用 XSLT 显示 XML 文档:http://www.w3schools.com/xml/xml_xsl.asp .打开 XML 文档时,将在浏览器中完成 XSLT 到 HTML 的转换。
现在,我在 a) 获取本地名称和 2) 在两个单独的表达式中获取 QName 类型的属性内容的命名空间方面遇到困难。
示例
<service xmlns:ns3="http://www.mycompany.com/" name="ns3:PersonService">
<serviceInterface name="ns3:PersonServiceInterface">
<operation>...</operation>
</serviceInterface>
问题
- 什么 XPATH 表达式将返回 PersonService 作为属性 name 的内容?
<xsl:value-of select="@name"/>
返回 ns3:PersonService 但我不想要 namespace 前缀。 - 什么 XPATH 表达式将返回 http://www.mycompany.com/ 作为属性name 的命名空间?
最佳答案
1.What XPATH expression will return PersonService as content of attribute name?
假设 service
是顶级元素的子元素(您没有提供完整且格式正确的 XML 文档),使用:
substring-after(/*/service/@name, ':')
2.What XPATH expression will return http://www.mycompany.com/ as the namespace of the attribute name?
在与上述相同的假设下,使用:
/*/service/namespace::*[name() = substring-before(../@name, ':')]
基于 XSLT 的验证:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="substring-after(/*/service/@name, ':')"/>
============
<xsl:value-of select=
"/*/service/namespace::*[name() = substring-before(../@name, ':')]"/>
</xsl:template>
</xsl:stylesheet>
此 XSLT 转换应用于以下 XML 文档时(已提供,但已完成):
<t>
<service xmlns:ns3="http://www.mycompany.com/"
name="ns3:PersonService">
<serviceInterface name="ns3:PersonServiceInterface">
<operation>...</operation>
</serviceInterface>
</service>
</t>
计算两个 XPath 表达式并将这些计算的结果复制到输出中:
PersonService
============
http://www.mycompany.com/
关于xslt - 用于获取数据类型 QName 的 XML 属性的本地名称和命名空间的 XPATH 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732422/