Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 2nd, 2015, 05:43 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Using saxon:parse() on CDATA Section

Hi, I have some input XML which contains nested XML in a CDATA section, as so:
Code:
<body>
<![CDATA[
<name>Bob</name>
<age>34</age>
]]>
</body>
I would like to be able to use XPath to interrogate the XML inside the CDATA and from what I've read saxon:parse() is the way to go (as we are using the Saxon processor).

Currently in my stylesheet I set a variable as so:
Code:
<xsl:variable name="data">
<xsl:value-of select="//body" />
</xsl:variable>
Note I have tried this with the disable-output-escaping="no" attribute on value-of as well.

I then set another variable as so:
Code:
<xsl:variable name="final" select="saxon:parse($data)" />
Outputting the value of $final just gives me a lot of blank lines.
Outputting the value of $data I can see that the XML is now escaped i.e.:
Code:
&lt;name&gt;Bob&lt;/name&gt;
&lt;age&gt;34&lt;/age&gt;
I cannot unescape the XML before passing it into saxon:parse(), the disable-output-escaping option only seems to affect how it looks rather than how it is treated.

If I add the disable-output-escaping attribute to the output of the $data variable, it is then unescaped.

Is anyone able to advise if I'm missing a step, or if it depends on how the transform is being processed? In this case I'm using Eclipse with Orangevolt and processor Saxon-B 8.9.

Any help appreciated, thanks in advance.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old June 2nd, 2015, 05:57 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Are you sure you don't get a parse error with saxon:parse($data) as your markup lacks a root element? In Saxon 9.6 you could use parse-xml-fragment http://www.w3.org/TR/xpath-functions...e-xml-fragment, but I don't think it exists in 8.9 (which is very old). If you want to successfully parse the data you have with saxon:parse then I would try
Code:
saxon:parse(concat('&lt;root>', $data, '&lt;/root>'))
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 2nd, 2015, 06:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm a little surprised this works because according to the spec at http://www.saxonica.com/documentatio...ns/saxon/parse the input to saxon:parse must be well-formed XML, that is, there must be an outermost element that wraps everything else. So I would expect a dynamic error. But 8.9 is a rather old release and I don't have its documentation handy.

I would suggest using the XSLT 3.0 parse-xml-fragment() function in preference: see http://www.w3.org/TR/xpath-functions...e-xml-fragment For that you'll need a more up-to-date Saxon release.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 2nd, 2015, 06:06 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Sorry, that input XML was just an example.
The actual XML I'm using does have a root element wrapping everything else, hence no error.

Unfortunately I'm limited to XSLT 2.0 in this case.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old June 2nd, 2015, 06:28 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you post minimal but complete samples of XSLT and XML that show the problem and allow us to reproduce it? You haven't shown what you do in case of "Outputting the value of $final just gives me a lot of blank lines". Show us the code you have, the output you want and the one you get.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 2nd, 2015, 09:30 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

It appears I have "rubber-ducked" this issue.
As I was posting the actual XML and XSLT causing the issue I realised that I was using a value-of in the wrong place (and therefore getting the values of the parsed XML rather than the nodes).

For reference or in case anyone else makes the same mistake, the variables I'm now setting are:
Code:
<xsl:variable name="requestXML" select="saxon:parse(//xrt:RequestXML)" />
<xsl:variable name="polData" select="$requestXML/PolMessage/PolData[@Type='Input']" />
where $polData is setting the root so further XPath is all relative.

Thanks for your assistance in getting me to this point.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a CDATA section; add nodeset to CDATA section kssudhish XSLT 3 January 3rd, 2008 07:13 AM
CDATA Section using XSLT misanaka XSLT 2 March 29th, 2007 06:53 PM
XSD CDATA Section chrisjonmcdonald XML 4 July 12th, 2006 08:15 AM
Load CDATA Section into DataSet monuindia2002 XML 1 April 25th, 2006 05:14 AM
CDATA section kai77 Beginning VB 6 0 October 31st, 2004 04:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.