2016-04-25 76 views
0

首先,让我提供示例xml,以便您清楚之后会发生什么。如何使用XSLT 1.0复制XML中的特定元素

<a>1</a> 
<b>1</b> 
<c>1</c> 
<d>1</d> 
<e>1</e> 
<f>1</f> 

是可以复制节点从a到b和从e到f。我需要忽略节点c和d。

<xsl:copy>它可以复制元素,但我需要从原始XML中获取特定元素。

谢谢。

+0

你的问题不明确。请显示转换的预期输出 - 并且不要在每个节点中使用具有相同值的示例。 –

回答

0

确定您可以删除所需元素。只需在标识转换后在指定元素上写入空模板即可。

源XML

<root> 
    <a>1</a> 
    <b>1</b> 
    <c>1</c> 
    <d>1</d> 
    <e>1</e> 
    <f>1</f> 
</root> 

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:strip-space elements="*"/> 

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

    <!-- Empty Template to Remove Elements --> 
    <xsl:template match="c|d"/> 

</xsl:stylesheet> 

输出XML

<root> 
    <a>1</a> 
    <b>1</b> 
    <e>1</e> 
    <f>1</f> 
</root> 

可替换地,选择部分icular节点保持:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:strip-space elements="*"/> 

    <!-- Root Template Match -->  
    <xsl:template match="root"> 
    <xsl:copy> 
     <xsl:apply-templates select="a|b|e|f"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Select Particular Elements --> 
    <xsl:template match="a|b|e|f"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet>