Xml 如何将平面列表转换为树
我有以下xml:Xml 如何将平面列表转换为树,xml,xslt,Xml,Xslt,我有以下xml: <root> <Element1> <Hierarchy attr="value"/> <Hierarchy attr="value"/> <Hierarchy attr="value"/> <Hierarchy attr="value"/> </Element1> <AnotherElement>
<root>
<Element1>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
</Element1>
<AnotherElement>
<YetAnotherElement>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
<Hierarchy attr="value"/>
</YetAnotherElement>
</AnotherElement>
</root>
如何将“层次结构”消息转换为树?i、 e
<root>
<Element1>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"/>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</Element1>
<AnotherElement>
<YetAnotherElement>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"/>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</YetAnotherElement>
</AnotherElement>
</root>
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Hierarchy[position() != 1]"/>
<xsl:template match="Hierarchy[1]">
<xsl:apply-templates select="." mode="Hierarchy"/>
</xsl:template>
<xsl:template match="Hierarchy" mode="Hierarchy">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="following-sibling::Hierarchy[1]" mode="Hierarchy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果:
<root>
<Element1>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</Element1>
<AnotherElement>
<YetAnotherElement>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</YetAnotherElement>
</AnotherElement>
</root>
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Hierarchy[position() != 1]"/>
<xsl:template match="Hierarchy[1]">
<xsl:apply-templates select="." mode="Hierarchy"/>
</xsl:template>
<xsl:template match="Hierarchy" mode="Hierarchy">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="following-sibling::Hierarchy[1]" mode="Hierarchy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果:
<root>
<Element1>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</Element1>
<AnotherElement>
<YetAnotherElement>
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value">
<Hierarchy attr="value"></Hierarchy>
</Hierarchy>
</Hierarchy>
</Hierarchy>
</YetAnotherElement>
</AnotherElement>
</root>
您只是对缩进感兴趣吗?大多数编辑器/生成器都会将相同深度的元素放入相同的列中以提高可读性,因为就文档模型而言,它们处于相同的级别。如果您担心的是缩进,那么提供您正在使用的编辑器或创建此XML的生成器可能会有所帮助。您只是对缩进感兴趣吗?大多数编辑器/生成器都会将相同深度的元素放入相同的列中以提高可读性,因为就文档模型而言,它们处于相同的级别。如果您担心的是缩进,那么提供您正在使用的编辑器或创建此XML的生成器可能会有所帮助。@Dimitre:+1。这真是一个成就,哈@迪米特里:+1。这真是一个成就,哈!