Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT 1

Status
Not open for further replies.

barguast2

Programmer
Oct 31, 2008
3
0
0
GB
I'm not so good with writing XSLT, so hopefully someone can help.

Basically what I need is a rule which states that if the input XML document has a certain root element, then output the same document unchanged.

Anyone? :)
 
You can simply devise an identity transformation in the xsl documnent and an extinction transformation - or nothing at all,say - herein-below, whereby when the root element's name or local-name match the target name, invoke the identity transformation, otherwise invoke the extinction transformation for parallelism. I use for the demo a param to store the target root local-name.
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:eek:utput method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="@*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="copy" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" mode="extinct">
</xsl:template>
<xsl:template match="/*">
<xsl:choose>
<xsl:when test="local-name()=$targetroot">
<xsl:apply-templates select="." mode="copy" />
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates select="." mode="extinct" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Amendment

Upon re-reading my post, I seem to have forgotten to insert the xst:param line as advertised. Here is a re-list.
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:eek:utput method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes" />
[red]<xsl:param name="targetroot">root</xsl:param>[/red]
<xsl:template match="@*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="copy" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()" mode="extinct">
</xsl:template>
<xsl:template match="/*">
<xsl:choose>
<xsl:when test="local-name()=$targetroot">
<xsl:apply-templates select="." mode="copy" />
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates select="." mode="extinct" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top