Click to See Complete Forum and Search --> : XSLT & copy-of: problem


CaptainPinko
06-16-2005, 06:04 PM
I have a XML fragment that looks like this:

...
<paragraph heading="Introduction">
Before you begin you should know:
-thing 1<br/>
-thing 2<br/>
-thing 3<br/>
</paragraph>
...


Now from within an XSLT I'd like to grab the contents of the paragraph __including__ the <br/> tags. When I try:


<xsl:for-each select=".">
<p><xsl:value of select="."/></p>
</xsl:for-each>


I lose the <br/> tags. When I tried replacing the <value-of> with <copy-of> I get the whole text between (and including) the <paragraph> tags.

How do I just the contents? I've taken a look at the different axes but those don't seem to help. As a last resort I wrap the <br> around the text but that doesn't seem to be too elegant a solution.

joodas
06-17-2005, 09:12 AM
What you need is all child nodes of the paragraph element to get copied, not the paragraph element itself.

The following line should do the trick.

<xsl:copy-of select="//paragraph/node()"/>

Note use of the node() in path, which instructs the XSLT processor to loop over all nodes, including the text ones. If you only used //paragraph/*, you would end up with <br/> elements alone, with no child text nodes copied.

CaptainPinko
06-20-2005, 02:31 PM
Hmm, is there a reason I don't the node() function listed on here http://www.devguru.com/Technologies/xslt/quickref/xslt_index_functions.html ?

joodas
06-21-2005, 04:28 AM
One good reason is that node() is not a function. It is a template rule pattern (http://www.w3.org/TR/xslt#patterns) , which matches child nodes of an element. Actually, it does match all nodes except the root one and the attribute elements. If you also want to get the attributes copied, you need to combine it with @* pattern.