且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

结合 xml、xpath 或 xquery

更新时间:2022-11-28 17:23:15

这个例子有点含糊.假设您的输入可以有多个 parent 节点,每个节点链接到多个 child 节点,我建议您使用 key 解决交叉引用:

The example is a little ambiguous. Assuming your input can have multiple parent nodes, each linked to multiple child nodes, I would suggest you use a key to resolve the cross-references:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="child" match="child" use="parentId" />

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="parent"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="parent">
    <xsl:copy>
        <xsl:copy-of select="*"/>
        <xsl:apply-templates select="key('child', parentId)"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="child">
    <xsl:copy>
        <xsl:copy-of select="*[not(self::parentId)]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>