2

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!

2
  • What do you mean by "output"? You mean "include in the result document"? Commented Dec 10, 2011 at 22:23
  • yes, sorry i am not used to functional programming may use wrong words :) Commented Dec 10, 2011 at 23:17

2 Answers 2

3
<xsl:copy-of select="$variable"/>
Sign up to request clarification or add additional context in comments.

2 Comments

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 form output but want to output c? thanks!
@user446141: See in my answer solutions to both of your questions. :)
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.