我尝试使用xsl:element创建节点,使其包含当前选择的所有属性。我认为这应该很简单,但是由于某种原因,我无法获得结果。
我隔离了情况,有XML:
<?xml-stylesheet href="myfile.xsl" type="text/xsl" ?>
<a firstAtt="1" secondAtt="2">
Some Text
</a>
我期望得到这个结果:
<a firstAtt="1" secondAtt="2"/>
有我的XSLT转换代码:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:element name="{name()}" use-attribute-sets="{@*}"/>
</xsl:template>
</xsl:stylesheet>
[不做深入探讨,我就尝试过这种方法。 (包括xsl:for-each
,xsl:attribute
等)
我可以理解,use-attribute-sets
不能那样工作(意味着在运行时定位到当前属性集)。也许我错了。
这是一个评估,并且有一个限制,不要使用xsl:copy。也有使其在XSLT 1.0中运行的限制
我是XML的新手-XSL。在此先感谢您的帮助和帮助。
我不知道我是否正确理解了“当前选择”的含义,但是此XSL样式表可以完成您期望的工作,至少对于您提供的输入是:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>