XSL Transformation Choose only the elements under the root tag without child nodes
I have a XML file like following and i want to select only the rootelements and no child elements with a match template. Can somebody help me how to write a match template which only selects the root elements from it ? Thanks for your help.
<root>
<child1>
<element1>Valule</element1>
<element2>开发者_Go百科;Value</element2>
</child1>
<child2>
<element1>Value</element1>
<element2>Value</element2>
</child2>
<rootelement1>rootval1</rootelement1>
<rootelement2>rootval2</rootelement2>
</root>
To select only the elements under the root tag without child elements, you can do the following
<xsl:apply-templates select="/root/*[not(*)]" />
Or, if you were currently positioned on the root element, it would be simplified to the following:
<xsl:apply-templates select="*[not(*)]" />
So, for example, with the following XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:apply-templates select="*[not(*)]" />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to you sample XML, the following is returned
<rootelement1>rootval1</rootelement1>
<rootelement2>rootval2</rootelement2>
精彩评论