我正在尝试转换一个类似于这个的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Records xmlns="http://some.place.net">
<Record>
<length unit="in">96</length>
<width unit="in">3.75</width>
<height unit="in">1.75</height>
<weight unit="lbs">8</weight>
</Record>
</Records>变成了类似于的东西
<?xml version="1.0" encoding="UTF-8"?>
<Records xmlns="http://some.place.net">
<Record>
<length>96</length>
<lengthunit>in</lengthunit>
<width>3.75</width>
<widthunit>in</widthunit>
<height>1.75</height>
<heightunit>in</heightunit>
<weight>8</weight>
<weightunit>lbs</weightunit>
</Record>
</Records>我的xlst样式表如下所示。我不知道如何将新元素显示为前面的元素的同级元素。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.something.com">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vNamespace" select="namespace-uri(/*)"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@unit">
<xsl:element name="{name(..)}{name()}" namespace="{$vNamespace}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>这才是我最后的结果。
<Records xmlns="http://some.place.net">
<Record>
<length>
<lengthunit>in</lengthunit>96</length>
<width>
<widthunit>in</widthunit>3.75</width>
<height>
<heightunit>in</heightunit>1.75</height>
<weight>
<weightunit>lbs</weightunit>8</weight>
</Record>
</Records>发布于 2015-10-06 05:09:59
如果选择元素而不是属性,则会更简单:
<xsl:template match="*[@unit]">
<xsl:element name="{name()}" namespace="{$vNamespace}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:element name="{name()}unit" namespace="{$vNamespace}">
<xsl:value-of select="@unit"/>
</xsl:element>
</xsl:template>https://stackoverflow.com/questions/32959678
复制相似问题