XSLT: transforming a document into itself

S

samppi

I'm teaching myself XSLT right now, but, just as part of learning, I'm
trying to figure out how one would transform a document an identical
document.

This doesn't work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

<xsl:template match="/">

<xsl:value-of select="/"/>

</xsl:template>

</xsl:stylesheet>

....but I can't figure out how else one would do it.

Thanks in advance!
 
M

Martin Honnen

samppi said:
I'm teaching myself XSLT right now, but, just as part of learning, I'm
trying to figure out how one would transform a document an identical
document.

This doesn't work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">

<xsl:template match="/">

<xsl:value-of select="/"/>

Well xsl:value-of outputs the string value of the selected node. If you
want to copy the complete document then use
<xsl:copy-of select="/"/>
instead. That copies everything the XSLT/XPath data model knows about.


You should also learn about the identity transformation template
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
as that also copies everything but allows you to add templates to
override the behaviour e.g. to change 'foo' elements to 'bar' elements
you simply add
<xsl:template match="foo">
<bar>
<xsl:apply-templates select="@* | node()"/>
</bar>
</xsl:template>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top