![](/img/trans.png)
[英]How to copy just a part of an attribute to the new XML using XSLT 1.0
[英]How to copy particular element in an XML using 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中獲取特定的元素。
謝謝。
當然,您可以刪除所需的元素。 身份轉換后,只需在指定的元素上寫空模板即可。
源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>
或者,選擇要保留的特定節點:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.