从我在上一个问题中发布的样式表构建,在元素声明中,您可以遍历每个Attributes/Attribute
元素并为您正在构建的元素构造属性。
您“站在”Object
该 for 循环内的元素节点上,因此您可以像这样迭代它的Attributes/Attribute
元素:
<xsl:for-each select="Attributes/Attribute">
<xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
</xsl:for-each>
应用于您的样式表:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:for-each select="Page/Object">
<xsl:element name="{Type}" >
<xsl:for-each select="Attributes/Attribute">
<xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
</xsl:for-each>
<xsl:value-of select="Value"/>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这是实现相同输出的另一种方法,但以一种更夸张的方式,使用apply-templates
代替for-each
.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates select="Page/Object" />
</body>
</html>
</xsl:template>
<xsl:template match="Object">
<xsl:element name="{Type}" >
<xsl:apply-templates select="Attributes/Attribute" />
<xsl:apply-templates select="Value" />
</xsl:element>
</xsl:template>
<xsl:template match="Attribute">
<xsl:attribute name="{@name}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
</xsl:stylesheet>