Xml XSLT:复制所有内容,但“剪切并粘贴”某些内容 出身背景

Xml XSLT:复制所有内容,但“剪切并粘贴”某些内容 出身背景,xml,xslt,Xml,Xslt,我的问题的一个更一般的版本是,但由于帖子的质量,没有收到回复。这是我的尝试 我有以下要转换的XML结构 预处理的XML 我想我的出发点是对的——用身份模板复制所有内容,然后处理我想要更改的内容。问题是我的第二个模板只返回复制的元素!这场比赛似乎效果很好,我只是丢失了我试图保存的所有其他内容。一般来说,使用for each并不是最好的主意。这使得模板不灵活且难以阅读。当然,有时你无法避免,但在这种情况下,这是可能的。您应该通过使用应用模板将迭代留给XSLT引擎。我会这样做: <xsl:tem

我的问题的一个更一般的版本是,但由于帖子的质量,没有收到回复。这是我的尝试

我有以下要转换的XML结构

预处理的XML 我想我的出发点是对的——用身份模板复制所有内容,然后处理我想要更改的内容。问题是我的第二个模板只返回复制的元素!这场比赛似乎效果很好,我只是丢失了我试图保存的所有其他内容。

一般来说,使用for each并不是最好的主意。这使得模板不灵活且难以阅读。当然,有时你无法避免,但在这种情况下,这是可能的。您应该通过使用应用模板将迭代留给XSLT引擎。我会这样做:

<xsl:template match="@* | node()" priority="-1"> <!-- copy everything unless another template with higher priority says otherwise -->
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
</xsl:template>

<xsl:template match="schema"> 
   <xsl:copy> <!-- copy the <schema> element -->
    <xsl:apply-templates select="complexType"/> <!-- copy all complexType elements within schema -->
   </xsl:copy>
</xsl:template>

<xsl:template match="element[contains(@type, '.')]"> <!-- if we encounter an <element> whose name contains the '.' character -->
        <xsl:variable name="type" select="@type" />
    <xsl:apply-templates select="//element[@name=$type]"/> <!-- copy the matching element instead -->
</xsl:template>
更新:我刚刚意识到我把@type和@name弄混了,修正了这个问题。

一般来说,使用for each并不是最好的主意。这使得模板不灵活且难以阅读。当然,有时你无法避免,但在这种情况下,这是可能的。您应该通过使用应用模板将迭代留给XSLT引擎。我会这样做:

<xsl:template match="@* | node()" priority="-1"> <!-- copy everything unless another template with higher priority says otherwise -->
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
</xsl:template>

<xsl:template match="schema"> 
   <xsl:copy> <!-- copy the <schema> element -->
    <xsl:apply-templates select="complexType"/> <!-- copy all complexType elements within schema -->
   </xsl:copy>
</xsl:template>

<xsl:template match="element[contains(@type, '.')]"> <!-- if we encounter an <element> whose name contains the '.' character -->
        <xsl:variable name="type" select="@type" />
    <xsl:apply-templates select="//element[@name=$type]"/> <!-- copy the matching element instead -->
</xsl:template>
更新:我刚刚意识到我把@type和@name搞混了,修正了这个问题

<xsl:template match="@* | node()">

        <xsl:copy>

            <xsl:apply-templates select="@* | node()" />

        </xsl:copy>

</xsl:template>

<xsl:template match="schema">

    <xsl:for-each select="element">

        <xsl:variable name="self" select="current()" />

        <xsl:variable name="name" select="@name" />

        <xsl:for-each select="/schema/complexType//element[@type=$name]">   

            <xsl:copy-of select="$self" />

        </xsl:for-each>         

    </xsl:for-each>

</xsl:template>
<xsl:template match="@* | node()" priority="-1"> <!-- copy everything unless another template with higher priority says otherwise -->
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
</xsl:template>

<xsl:template match="schema"> 
   <xsl:copy> <!-- copy the <schema> element -->
    <xsl:apply-templates select="complexType"/> <!-- copy all complexType elements within schema -->
   </xsl:copy>
</xsl:template>

<xsl:template match="element[contains(@type, '.')]"> <!-- if we encounter an <element> whose name contains the '.' character -->
        <xsl:variable name="type" select="@type" />
    <xsl:apply-templates select="//element[@name=$type]"/> <!-- copy the matching element instead -->
</xsl:template>