XSLT: getting text of a node into an attribute

Kevin (39)

Usenet Archive
Joined
Jul 31, 2003
Messages
3
Reaction score
0
I have a document, lets say:

<Doc>
<Ele1 Att1='hello'>
<Ele2 Att2='goodbye'/>
</Ele1>
</Doc>

In XSLT I need to generate an attribute value which contains part of this document as a text string, ie:

<MyOutput XML="&lt;Ele1 Att1='hello'>
&lt;Ele2 Att2='goodbye'/>
&lt;/Ele1>"/>

I can output it easily enough at child elements (copy-of), or as a CDATA string in the element content, but I cannot find any way to get it into an attribute.

What should I be doing?
 

Andy Fish

Usenet Archive
Joined
Dec 30, 2002
Messages
605
Reaction score
0
use

<xsl:attribute name="XML">
<xsl:copy-of ...> (or whatever)
</xsl:attribute>

anything you put inside here will be put into the XML attribute of the node you're creating
 

Kevin Burges

Usenet Archive
Joined
Aug 4, 2003
Messages
3
Reaction score
0
Unfortunately not, I already tried that. For some reason nothing comes through in the attribute if I do <copy-of>. I just end up with an empty attribute.

I'm guessing copy-of outputs a nodeset rather than a string, hence you can't stick it into an attribute value.

Any other ideas?
 

Andy Fish

Usenet Archive
Joined
Dec 30, 2002
Messages
605
Reaction score
0
looking at your original post, I see that I misunderstood what you meant by 'text of a node'. in XML, text is stuff that isn't inside an element or comment. what you wanted was a textual representation of an element i.e.
with the angle brackets in.

The angle bracket notation is something that is used to serialise XML so that it can be saved or viewed, but programs that process XML (including the XSLT processor) don't see the angle brackets when doing the processing; they are working with a tree of nodes. The angle brackets are only added on at the end if you're using an output method that requires them.

So what I'm sating is that if you want to include a piece of serialized XML in the output document you'll have to do it yourself. something like:

<xsl:template match="Doc/*">
<xsl:element name="MyOutput">
<xsl:attribute name="XML">
&lt;
<xsl:value-of select="name"/>
<xsl:for-each select="@*">
 
<xsl:value-of select="name"/> = &quot;<xsl:value-of
select="text/>&quot;
&gt;
</xsl:attribute>
</xsl:element>
</xsl:template>

this is untested but I think it should give you the idea

Andy
 

Kevin Burges

Usenet Archive
Joined
Aug 4, 2003
Messages
3
Reaction score
0
Thanks for your help, that sounds like it will probably do the trick. I thought I might have to do something like that, but I was hoping there would be a way around it.
 

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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top