2017-02-24 50 views
0

如何在xslt上执行条件副本。例如基于子元素值的重复子节点

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

所以,如果名称= “约翰”,那么:

<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> 

这也使得“简副本“如何有条件地复制这个?

回答

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>