以下樣式表將刪除有孩子(除了根元素)的所有元素,只返回葉節點的「平坦」的文章:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/*[*]">
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>
當施加到下面測試輸入:
<world>
<Europe>
<Germany>
<Berlin>no</Berlin>
<Munich>yes</Munich>
</Germany>
<France>
<Paris>no</Paris>
</France>
<Italy>
<Rome>no</Rome>
<Venice>yes</Venice>
<Milano>no</Milano>
</Italy>
</Europe>
<Asia>
<China>
<Beijin>no</Beijin>
<Shanghai>yes</Shanghai>
</China>
<India>
<Mumbay>no</Mumbay>
</India>
</Asia>
<America>
<USA>
<NewYork>
<NewYork>no</NewYork>
<Albany>yes</Albany>
</NewYork>
<California>
<LosAngeles>no</LosAngeles>
<SanFranciso>no</SanFranciso>
</California>
</USA>
<Canada>
<Vancouver>no</Vancouver>
<Montreal>yes</Montreal>
</Canada>
<Mexico>
<Tijuana>no</Tijuana>
</Mexico>
</America>
</world>
的結果將爲:
<?xml version="1.0" encoding="UTF-8"?>
<world>
<Berlin>no</Berlin>
<Munich>yes</Munich>
<Paris>no</Paris>
<Rome>no</Rome>
<Venice>yes</Venice>
<Milano>no</Milano>
<Beijin>no</Beijin>
<Shanghai>yes</Shanghai>
<Mumbay>no</Mumbay>
<NewYork>no</NewYork>
<Albany>yes</Albany>
<LosAngeles>no</LosAngeles>
<SanFranciso>no</SanFranciso>
<Vancouver>no</Vancouver>
<Montreal>yes</Montreal>
<Tijuana>no</Tijuana>
</world>
您的源XML格式不正確。它是否有根元素?如果是這樣,請編輯您的問題以包含它。而且,由於您決定使用XSLT,請包括您迄今爲止所嘗試的內容。 – helderdarocha