开发者

XSL: Modify contents within XML

开发者 https://www.devze.com 2023-03-20 23:21 出处:网络
I have a XML in which there is another XML (within CDATA). Now I want to modify the content of child XML. How is it possible?

I have a XML in which there is another XML (within CDATA). Now I want to modify the content of child XML. How is it possible?

In the below XML, If Address Type is Home, I want to change it to "01". Is it possible i开发者_开发技巧n XSLT 1.0??

<?xml>
 <a>
   <b>This is Parent</b>
   <c>
     <![CDATA[
       <?xml>
         <a1>This is Child XML></a1>
         <person_address type="Home">
           <street>ABCDStreet</street>
           <city/>
           <country/>
         </person_address>
     ]]>
  </c>
</a>


As described in the linked question above in the comment, you can't treat the content of CDATA as XML, it is pure text.

First easy solution coming into mind (not saying the best one): in your case (XSLT 1.0 and simple text replacement) you could use some EXSLT extension template like replace() to match the string against a regex and replace it with the wanted value.


You need to extract the text of the CDATA section and pass it to an XML parser for processing. Some XSLT processors have an extension function to do this, for example saxon:parse(), or you may be able to write your own in Java or Javascript, for example.


Anything contained in a CDATA section isn't markup -- it is just 1-dimensional text.

Either process it as text (which is ugly and inconvenient), or write an extension function that parses its argument to an XmlDocument and returns this result back. Then you can process the result with XSLT:

 <xsl:apply-templates select="my:parse(theString)/*"/>

Of course, for this to work you must ensure that the text passed to the my:parse() extension function is a well-formed (serialization of) XML document -- which in your example it isn't.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号