0

I am trying to add an attribute to a node which is part of a CDATA. My XML is

 <documents>
   <document id="1234">
    <field name="CDATA" value="bill"><![CDATA[<bill name="xxx" age="12" />]]> </field>
   </document>
 </documents>

The result xml should be

<docs>
  <doc>
   <bill name="xxx" age="12" id="1234" />
  </doc>
</docs>

The id attribute in the document node should be added as an attribute to the node inside the CDATA.

I was able to get the CDATA value as field[@name='CDATA'] but I was not sure how to add the attribute of document to the cdata..

Can anyone please suggest me how to do this or point me in the right direction?

2
  • Please edit your post and show the XSLT you are using. Commented Aug 6, 2012 at 23:51
  • a CDATA section is just a part of a text node. As such it only contains text and no nodes (even if the text seems the same as the textual serialization of a node). Therefore, what you want isn't possible in pure XSLT 1.0 and 2.0, unless you also have an XML parser written in XSLT. In XSLT 3.0/XPath 3.0 (still drafts) there might be a function: parse-xml() that can be used to parse the text and create from it an XML document/fragment: w3.org/TR/xpath-functions-30/#func-parse-xml Commented Aug 7, 2012 at 2:08

2 Answers 2

1

A CDATA section is just a part of a text node.

As such it only contains text and no nodes (even if the text seems the same as the textual serialization of a node).

Therefore, what you want isn't possible in pure XSLT 1.0 and 2.0, unless you also have an XML parser written in XSLT.

In XSLT 3.0/XPath 3.0 (still drafts) there might be a function: parse-xml() that can be used to parse the text and create from it an XML document/fragment:

Sign up to request clarification or add additional context in comments.

Comments

1

CDATA means "the stuff in here is to be treated as plain text even if it looks like markup". CDATA is an explicit instruction to the parser to ignore markup within the content. That is the only purpose of the CDATA tags. Therefore, there are no nodes inside a CDATA section by definition.

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.