我正在尝试获取XSLT中带前缀的XML标记数据的值。
示例 XML:
<?xml version="1.0" encoding="utf-8"?>
<emp xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://www.w3.org/1999/XSL/dataservices/metadata" xmlns:d="http://www.w3.org/1999/XSL/dataservices">
<record>
<info type="application/xml">
<m:employee>
<d:name>11278</d:name>
</m:employee>
</info>
</record>
</emp>
我想使用 XSLT 获取name
元素的数据。我尝试了不同的选择。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.w3.org/1999/XSL/dataservices/metadata"
xmlns:d="http://www.w3.org/1999/XSL/dataservices">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template match="/">
<html>
<body>
<h2><i>Emp Compensastion Data</i></h2>
<div class="content-holder">
<div>Name of the employee: <xsl:value-of select="emp/record/info/m:employee/d:name"/></div>
<div>Name of the Employee: <xsl:value-of select="//*[local-name()='m:employee']/*[local-name()='d:name']"/></div>
<div>Name of the Employee: <xsl:value-of select="//*[name()='m:employee']/*[name()='d:name']"/></div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我是 XSL 转换的新手。请帮忙。
注意:我尝试使用 XSL 转换创建 HTML。
<xsl:value-of select="emp/record/info/m:employee/d:name"/>
这是你最好的尝试。它不起作用的原因是您的 XML 定义了默认命名空间xmlns="http://www.w3.org/2005/Atom"
并且所有不带前缀的元素(如emp
、record
和info
(都在此命名空间中。
您必须在样式表中声明此命名空间,为其分配前缀,并在处理源 XML 中的无前缀元素时使用该前缀:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.w3.org/2005/Atom"
xmlns:m="http://www.w3.org/1999/XSL/dataservices/metadata"
xmlns:d="http://www.w3.org/1999/XSL/dataservices"
exclude-result-prefixes="a m d">
<xsl:output method='html' encoding='UTF-8'/>
<xsl:template match="/">
<html>
<body>
<h2><i>Emp Compensastion Data</i></h2>
<div class="content-holder">
<div>Name of the employee: <xsl:value-of select="/a:emp/a:record/a:info/m:employee/d:name"/></div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
注意:
您永远不必使用像*[local-name()='m:employee']
这样的黑客。但是您还应该知道,元素的本地名称不包括前缀 - 因此谓词永远不会为真。
如果我理解正确,您正在尝试三种不同的 xpath 来获取值,但没有任何效果。是吗?
<div class="content-holder">
<div>Name of the employee: <xsl:value-of select="emp/record/info/m:employee/d:name"/></div>
<div>Name of the Employee: <xsl:value-of select="//*[local-name()='m:employee']/*[local-name()='d:name']"/></div>
<div>Name of the Employee: <xsl:value-of select="//*[name()='m:employee']/*[name()='d:name']"/></div>
</div>
但是,我尝试了转换并得到了以下结果:
<?xml version = '1.0' encoding = 'UTF-8'?>
<html xmlns:d="http://www.w3.org/1999/XSL/dataservices" xmlns:m="http://www.w3.org/1999/XSL/dataservices/metadata" xmlns:ns0="http://www.w3.org/2005/Atom">
<body>
<h2><i>Emp Compensastion Data</i>
</h2>
<div class="content-holder">
<div>Name of the employee:</div>
<div>Name of the Employee:</div>
<div>Name of the Employee:11278</div>
</div>
</body>
</html>
因此,我可以断言第三个xpath正在工作。
第一个 xpath 不起作用,因为还有另一个问题:emp
元素位于由xmlns="http://www.w3.org/2005/Atom"
定义的命名空间中 这需要考虑,例如,我在样式表属性中添加了xmlns:ns0="http://www.w3.org/2005/Atom"
:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://www.w3.org/2005/Atom"
xmlns:m="http://www.w3.org/1999/XSL/dataservices/metadata" xmlns:d="http://www.w3.org/1999/XSL/dataservices">
现在我得到了以下第一个 xpath。它也适用于:
<div>Name of the employee:<xsl:value-of select="ns0:emp/ns0:record/ns0:info/m:employee/d:name"/></div>