必要な完全なスタイルシートは次のとおりです (名前空間が重要であるため)。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="foo">
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="z:row">
<xsl:variable name="A" select="@A" />
<xsl:for-each select="@*[local-name() != 'A']">
<z:row A="{$A}">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</z:row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
コードが短くなり、生成している結果ドキュメントの構造が見やすくなるため、可能な場合よりもリテラル結果要素 (例: <z:row>
)<xsl:element>
や属性値テンプレート (属性値のテンプレート) を使用することを好みます。 . 他の人はand を好みます。なぜなら、すべてが XSLT 命令だからです。{}
<xsl:attribute>
<xsl:element>
<xsl:attribute>
except
XSLT 2.0 を使用している場合、XPath の演算子と でselect
属性を直接使用する機能など、役立つ構文上の優れた点がいくつかあります<xsl:attribute>
。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns:z="foo">
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="z:row">
<xsl:variable name="A" as="xs:string" select="@A" />
<xsl:for-each select="@* except @A">
<z:row A="{$A}">
<xsl:attribute name="{local-name()}" select="." />
</z:row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>