您好,有人知道转换此 XML 的 xsl 是什么样子吗? pid之后可以有N个nte,pv1之后可以有N个nte。该结构保证pid之后的所有nte都属于pid,pv1之后的所有nte都属于pv1。
来自:
<pid>
</pid>
<nte>
<nte-1>1</nte-1>
<nte-3>Note 1</nte-1>
</nte>
<nte></nte>
<pv1></pv1>
<nte>
</nte>
进入:
<pid>
<nte>
<nte-1>1</nte-1>
<nte-3>Note 1</nte-1>
</nte>
<nte>
</nte>
</pid>
<pv1>
<nte>
</nte>
</pv1>
谢谢!
最佳答案
这个转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kLogicalChildren" match="nte"
use="generate-id(preceding-sibling::*
[self::pid or self::pv1]
[1])"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="pid|pv1">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select=
"key('kLogicalChildren', generate-id())"/>
</xsl:copy>
</xsl:template>
<xsl:template match="nte"/>
</xsl:stylesheet>
应用于提供的 XML 文档(已更正为格式正确):
<t>
<pid></pid>
<nte>
<nte-1>1</nte-1>
<nte-3>Note 1</nte-3>
</nte>
<nte></nte>
<pv1></pv1>
<nte></nte>
</t>
产生想要的、正确的结果:
<t>
<pid>
<nte>
<nte-1>1</nte-1>
<nte-3>Note 1</nte-3>
</nte>
<nte/>
</pid>
<pv1>
<nte/>
</pv1>
</t>
关于xml - 从兄弟节点创建子节点,直到出现不同的兄弟节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3026346/