XSLT 以特定格式转换 xml
XSLT to transform xml in specific format
现在的XML看起来像
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<attribute>
<name>Attr1</name>
<value>Attr1 Value 1</value>
</attribute>
<attribute>
<name>Attr2</name>
<value>Attr 2 Value 1</value>
<value>Attr 2 Value 2</value>
</attribute>
</document>
我希望新的 xml 看起来像以下....
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<Attr1>
<value>Attr1 Value 1</value>
</Attr1>
<Attr2>
<value>Attr 2 Value 1</value>
<value>Attr 2 Value 2</value>
</Attr2>
</document>
我想使用 xslt 来实现这种转换...我的 xslt 不起作用
解决方法如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/document">
<document>
<xsl:apply-templates />
</document>
</xsl:template>
<xsl:template match="/document/attribute">
<xsl:element name="{name}">
<xsl:copy-of select="value" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这样做的主要困难是您需要创建一个具有动态名称的元素。这就是 <xsl:element name="{name}">
的用途。您可以在大括号之间放置任何 xpath 表达式。这里我们想要 /document/attribute
匹配的(希望是唯一的)名称节点。
我用不同的方式来处理上面的问题...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="attribute">
<xsl:element name="{name}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="name"/>
</xsl:stylesheet>
现在的XML看起来像
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<attribute>
<name>Attr1</name>
<value>Attr1 Value 1</value>
</attribute>
<attribute>
<name>Attr2</name>
<value>Attr 2 Value 1</value>
<value>Attr 2 Value 2</value>
</attribute>
</document>
我希望新的 xml 看起来像以下....
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<Attr1>
<value>Attr1 Value 1</value>
</Attr1>
<Attr2>
<value>Attr 2 Value 1</value>
<value>Attr 2 Value 2</value>
</Attr2>
</document>
我想使用 xslt 来实现这种转换...我的 xslt 不起作用
解决方法如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/document">
<document>
<xsl:apply-templates />
</document>
</xsl:template>
<xsl:template match="/document/attribute">
<xsl:element name="{name}">
<xsl:copy-of select="value" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这样做的主要困难是您需要创建一个具有动态名称的元素。这就是 <xsl:element name="{name}">
的用途。您可以在大括号之间放置任何 xpath 表达式。这里我们想要 /document/attribute
匹配的(希望是唯一的)名称节点。
我用不同的方式来处理上面的问题...
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="attribute">
<xsl:element name="{name}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="name"/>
</xsl:stylesheet>