XML/XSLT 属性输出
XML/XSLT attribute output
我正在做一个 XML/XSLT 小项目,但我无法从 XML 文件中检索多个元素。
我想检索客户名称(属性)和交易金额(属性),但它不起作用。我唯一可以输出的元素属性是client name
。
我尝试将 <xsl:template match="client">
更改为 <xsl:template match="list">
,但后来一切都显示出来了,我不想打印问题元素。
输出名称和金额后,我需要对金额求和以显示总数,但首先我确实需要获取要输出的金额。有什么想法吗?
XML 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet href="xslt.xml"
type="application/xml"?>
<list>
<client name="Morton">
<transaction amount="43" />
<question>Who?</question>
<transaction amount="21" />
</client>
<client name="Mingus">
<transaction amount="6" />
<transaction amount="32" />
<question>Where?</question>
<transaction amount="45" />
</client>
</list>
XSLT 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="client">
<html>
<body>
<p>Name:</p>
<xsl:apply-templates select="client" />
<xsl:value-of select="client" />
<xsl:value-of select="@name" />
<p>Total:</p>
<xsl:value-of select="transaction" />
<xsl:value-of select="@amount" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当前输出结果:
Client:
Morton
Total:
Client:
Mingus
Total:
期望的输出结果:
Client: Morton
Total: 64
Client: Mingus
Total: 83
您遇到的问题是您的 xpaths。您的上下文是 client
所以所有路径都必须相对于它。因此,如果您尝试获取子 transaction
的 amount
属性,则 xpath 将为 transaction/@amount
.
但是,如果您使用 <xsl:value-of select="transaction/@amount"/>
并且有多个 transaction
个子项,您将只会获得第一次出现的值。 (无论如何在 XSLT 1.0 中。在 XSLT 2.0 中,您将获得所有连接在一起的值。)
我会这样做:
XML 输入
<list>
<client name="Morton">
<transaction amount="43" />
<question>Who?</question>
<transaction amount="21" />
</client>
<client name="Mingus">
<transaction amount="6" />
<transaction amount="32" />
<question>Where?</question>
<transaction amount="45" />
</client>
</list>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/list">
<html>
<head></head>
<body>
<xsl:apply-templates select="client"/>
</body>
</html>
</xsl:template>
<xsl:template match="client">
<p>Client: <xsl:value-of select="@name"/></p>
<p>Total: <xsl:value-of select="sum(transaction/@amount)"/></p>
</xsl:template>
</xsl:stylesheet>
HTML输出
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Client: Morton</p>
<p>Total: 64</p>
<p>Client: Mingus</p>
<p>Total: 83</p>
</body>
</html>
我正在做一个 XML/XSLT 小项目,但我无法从 XML 文件中检索多个元素。
我想检索客户名称(属性)和交易金额(属性),但它不起作用。我唯一可以输出的元素属性是client name
。
我尝试将 <xsl:template match="client">
更改为 <xsl:template match="list">
,但后来一切都显示出来了,我不想打印问题元素。
输出名称和金额后,我需要对金额求和以显示总数,但首先我确实需要获取要输出的金额。有什么想法吗?
XML 文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet href="xslt.xml"
type="application/xml"?>
<list>
<client name="Morton">
<transaction amount="43" />
<question>Who?</question>
<transaction amount="21" />
</client>
<client name="Mingus">
<transaction amount="6" />
<transaction amount="32" />
<question>Where?</question>
<transaction amount="45" />
</client>
</list>
XSLT 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="client">
<html>
<body>
<p>Name:</p>
<xsl:apply-templates select="client" />
<xsl:value-of select="client" />
<xsl:value-of select="@name" />
<p>Total:</p>
<xsl:value-of select="transaction" />
<xsl:value-of select="@amount" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当前输出结果:
Client:
Morton
Total:
Client:
Mingus
Total:
期望的输出结果:
Client: Morton
Total: 64
Client: Mingus
Total: 83
您遇到的问题是您的 xpaths。您的上下文是 client
所以所有路径都必须相对于它。因此,如果您尝试获取子 transaction
的 amount
属性,则 xpath 将为 transaction/@amount
.
但是,如果您使用 <xsl:value-of select="transaction/@amount"/>
并且有多个 transaction
个子项,您将只会获得第一次出现的值。 (无论如何在 XSLT 1.0 中。在 XSLT 2.0 中,您将获得所有连接在一起的值。)
我会这样做:
XML 输入
<list>
<client name="Morton">
<transaction amount="43" />
<question>Who?</question>
<transaction amount="21" />
</client>
<client name="Mingus">
<transaction amount="6" />
<transaction amount="32" />
<question>Where?</question>
<transaction amount="45" />
</client>
</list>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/list">
<html>
<head></head>
<body>
<xsl:apply-templates select="client"/>
</body>
</html>
</xsl:template>
<xsl:template match="client">
<p>Client: <xsl:value-of select="@name"/></p>
<p>Total: <xsl:value-of select="sum(transaction/@amount)"/></p>
</xsl:template>
</xsl:stylesheet>
HTML输出
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Client: Morton</p>
<p>Total: 64</p>
<p>Client: Mingus</p>
<p>Total: 83</p>
</body>
</html>