Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   output node with subnodes "as-is" (http://www.velocityreviews.com/forums/t168362-output-node-with-subnodes-as-is.html)

Kristofer Andersson 12-03-2004 08:20 AM

output node with subnodes "as-is"
 
I am using xslt to transform some parts of a document but want to
output some parts of the document just like they are (node with all
subnodes and attributes).

Below are the templates I am currently using. The only problem I have
is that the value of parent nodes will be the value of all subnodes ie
"<parentnode><childnode>value</childnode></parentnode>" will become
"<parentnode>value<childnode>value</childnode></parentnode>" after
transformation. Can I avoid this? Or can this be done in a more simple
way than the somewhat clumsy templates below?

<xsl:template match="mySubtree">
<xsl:element name="subtreeWrapperNode">
<xsl:apply-templates mode="subNodes" select="." />
</xsl:element>
</xsl:template>

<xsl:template mode="subNodes" match="*">
<xsl:for-each select="*">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."
/></xsl:attribute>
</xsl:for-each>
<xsl:value-of select="." />
<xsl:apply-templates mode="subNodes" select="." />
</xsl:element>
</xsl:for-each>
</xsl:template>

The Square Root of Negative One 12-03-2004 08:44 AM

Re: output node with subnodes "as-is"
 
Use xsl:copy-of.

<!-- Category: instruction -->
<xsl:value-of
select = string-expression
disable-output-escaping = "yes" | "no"/>

From: <http://www.w3.org/TR/xslt#value-of>

In article <17ee29ab.0412030020.34bb97be@posting.google.com >,
kristofera@gmail.com (Kristofer Andersson) wrote:

> I am using xslt to transform some parts of a document but want to
> output some parts of the document just like they are (node with all
> subnodes and attributes).
>
> Below are the templates I am currently using. The only problem I have
> is that the value of parent nodes will be the value of all subnodes ie
> "<parentnode><childnode>value</childnode></parentnode>" will become
> "<parentnode>value<childnode>value</childnode></parentnode>" after
> transformation. Can I avoid this? Or can this be done in a more simple
> way than the somewhat clumsy templates below?
>
> [code snipped...]


David Carlisle 12-03-2004 10:24 AM

Re: output node with subnodes "as-is"
 

as another respondentsaid, if you just want the whole subtree
unchanged then you can just do

<xsl:template match="mySubtree">
<xsl:copy-of select="."/>
</xsl:template>

and need nothing else. A more usual case is where you want templates
that do an identity transform but then have higher priority templates
for certain special attributes that do something other than copy.
even there your code is very verbose

<xsl:element name="{name()}">

you can use
<xsl:copy>

for that
<xsl:for-each select="@*">
<xsl:attribute name="{name()}"><xsl:value-of select="."
/></xsl:attribute>

that's

<xsl:copt-of select="@*"/>

<xsl:value-of select="." />
<xsl:apply-templates mode="subNodes" select="."
/>

That would mangle your input
the value-of gives the string value of the current node, which is all
the character content of all the descendants then you process all the
child elements again (so you'll get much of teh content again in a
typical case)

The XSLT spec has an example identity transform.

David



All times are GMT. The time now is 12:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.