Ataman
Ataman

Reputation: 2590

XSL output everythin including node name, attributes, text

On XSLT, Is there a simple way to output everything of a variable?

My variable is something like:

<node a="a">
  a
  <node>
    b
  </node>
</node>

So i want to output it as it is including tag names, attributes, text etc.

Thanks!

AND:

is it possible not to output some tags? such as

<a>aa
<b>bb
<c>cc</c></b></a> 

i want to avoid b tag from output but want to output c? thanks!

Upvotes: 2

Views: 154

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Good question, +1.

This transformation provides the answers to both your questions:

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

 <xsl:variable name="vVarNode" select="/*/node"/>

 <xsl:variable name="vVarA" select="/*/a"/>

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

 <xsl:template match="/">
  <xsl:copy-of select="$vVarNode"/>
  ===========

  <xsl:apply-templates select="$vVarA"/>
 </xsl:template>

 <xsl:template match="b">
  <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (from which the two variables are "loaded"):

<doc>
 <node a="a">
    a
    <node>
     b
    </node>
 </node>

 <a>aa
  <b>bb
    <c>cc</c>
  </b>
 </a>
</doc>

the wanted, correct result is produced (The content of the first variable is output "as-is", while b and its text-node children are "deleted" from what is output out of the content of the second variable):

<node a="a">
    a
    <node>
     b
    </node>
</node>
  ===========

  <a>aa
  <c>cc</c>
</a>

Upvotes: 1

Francis Avila
Francis Avila

Reputation: 31641

<xsl:copy-of select="$variable"/>

Upvotes: 3

Related Questions