在 XSLT 中获取前缀 XML 标签的值
Getting the value of prefixed XML tag in XSLT
我正在尝试获取 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']
这样的 hack。但是您还应该知道元素的本地名称不包括前缀 - 因此谓词将 never 为真。
如果我没理解错的话,您正在尝试三种不同的 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>
我正在尝试获取 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']
这样的 hack。但是您还应该知道元素的本地名称不包括前缀 - 因此谓词将 never 为真。
如果我没理解错的话,您正在尝试三种不同的 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>