将所有子节点转换为类父节点



有必要通过使所有子元素具有相同的层次级别和名称来将它们转换为父级节点。生成的新元素必须包含子元素的所有属性,并保留父元素的属性。

源XML

<?xml version="1.0" encoding="UTF-8"?>
<entry>
<line id="001" att1="aaa" att2="bbb" att3="ccc"/>
<line id="002" att1="ddd" att2="eee" att3="fff"/>
<line id="003" att1="ggg" att2="hhh" att3="iii">
<subline  x="name" z="lastname"/>
<subline  x="name2" z="lastname2"/>
<underline  a="bar" b="foo"/>
</line>
</entry>

期望输出值

<entry>
<line id="001" att1="aaa" att2="bbb" att3="ccc"/> <!-- with or without empty x and z attributes' values-->
<line id="002" att1="ddd" att2="eee" att3="fff"/> <!-- with or without empty x and z values-->
<line id="003" att1="ggg" att2="hhh" att3="iii" x="name" z="lastname"/>
<line id="003" att1="ggg" att2="hhh" att3="iii" x="name2" z="lastname2"/>
<line id="003" att1="ggg" att2="hhh" att3="iii" a="bar" b="foo"/>
</entry>

显示XSLT代码

当前代码只匹配第一个子元素。我想转换所有其他

<?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="xml" indent="yes"/>

<xsl:template match="line">
<line>
<xsl:copy-of select="@*"/>
<xsl:attribute name="x">
<xsl:value-of select="subline/@x"/>
</xsl:attribute>
<xsl:attribute name="z">
<xsl:value-of select="subline/@z"/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</line>
</xsl:template>


<!-- ===== delete child elements ====== -->
<xsl:template match="subline"/>
<xsl:template match="underline"/>

<!-- ===== [identity] ====== -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- ==== [identity  ] ====== -->

</xsl:stylesheet>

额外注意(可能有用):所有属性名都是预先知道的

我建议这样做:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="line[*]">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="line/*">
<line>
<xsl:copy-of select="../@* | @*"/>
</line>
</xsl:template>
</xsl:stylesheet>

最新更新