combining xml, xpath or xquery

后端 未结 1 653
不知归路
不知归路 2021-01-27 09:13
parentId1 aParentValue
相关标签:
1条回答
  • 2021-01-27 09:39

    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>
    
    0 讨论(0)
提交回复
热议问题