基于子元素值的重复子节点

时间:2017-02-24 15:18:53

标签: xslt

如何在xslt上执行条件复制。例如

<Person>
   <Name>John</Name>
   <Sex>M</Sex>
</Person>
<Person>
    <Name>Jane</Name>
    <Sex>F</Sex>
</Person>

所以如果Name =“John”那么:

<Person>
   <Name>John</Name>
   <Sex>M</Sex>
</Person>
<Copied>
   <Name>John</Name>
   <Sex>M</Sex>
</Copied>
<Person>
    <Name>Jane</Name>
    <Sex>F</Sex>
</Person>

到目前为止,我有一点xslt:

<xsl:template match="Person">
    <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    <Copied>
           <xsl:apply-templates select="node()|@*"/>
    </Copied>
</xsl:template>

这也是“jane”的副本如何有条件地复制这个?

1 个答案:

答案 0 :(得分:3)

你可以这样做:

<xsl:template match="Person">
    <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    <xsl:if test="Name='John'">
        <Copied>
               <xsl:apply-templates select="node()|@*"/>
        </Copied>
    </xsl:if>
</xsl:template>

或者也许:

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Person[Name='John']">
    <xsl:copy-of select="."/>
    <Copied>
           <xsl:copy-of select="*"/>
    </Copied>
</xsl:template>