Project

Profile

Help

Support #3186

closed

Saxon API: How to extract output from XdmDestination

Added by Sunny boddu about 7 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
-
Sprint/Milestone:
-
Start date:
2017-03-29
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

Description

Hi,

I am using SaxonAPI .Net version,

I have simple xsl and xml. I am using below code to transform the xml and expecting output.

I have attached xsl and xml files already.

The problem currently I am having is, The output in destination is not proper xml. The output xml contains "<" and ">" in xml instead of proper xml tags '<', '>'.

Can anyone help me what the causing the error?

Below is the code snippet I m using :

            var processor = new Processor();

            XsltCompiler xslComp = processor.NewXsltCompiler();

            XsltExecutable xslExec = xslComp.Compile(xslUri);


            XsltTransformer xslTrans = xslExec.Load();

XdmDestination xdmD = new XdmDestination();

            Serializer serializer = processor.NewSerializer();


            using (FileStream fs = File.Open(xmlPath, FileMode.Open))

            {

                xslTrans.SetInputStream(fs, schemaUri); // set baseUri

                xslTrans.Run(xdmD);

            }


            XdmNode value = null;


            if (xdmD != null)

            {

                value = xdmD.XdmNode;

            }

Current output looks like below:

<?xml version="1.0" encoding="ISO-8859-1"?>

<nf:rpc-reply xmlns:nf="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="http://www.cisco.com/nxos:1.0:vms">

<nf:data>

<show>

<svs>

&lt;connections&gt;

    &lt;conn-name&gt;qa-gdc1&lt;/conn-name&gt;        

&lt;/connections&gt;

</svs>

</show>

</nf:data>

</nf:rpc-reply>


Files

xsl.xsl (475 Bytes) xsl.xsl Input xsl Sunny boddu, 2017-03-29 16:24
xml.xml (362 Bytes) xml.xml Input xml Sunny boddu, 2017-03-29 16:24
output.xml (399 Bytes) output.xml Sunny boddu, 2017-03-29 16:27
output.PNG (45.5 KB) output.PNG Sunny boddu, 2017-03-29 16:31
Actions #1

Updated by Sunny boddu about 7 years ago

The actual output which I am having is not showing properly in this patch. Please refer attached output.xml .

'&lt' and '&gt' are still showing in output xml..... It is not showing as '<', '>'

Actions #2

Updated by Michael Kay about 7 years ago

You are attempting to write lexical XML with the code:

<xsl:value-of select="concat('&lt;?xml',substring-after(//root/text(),'&lt;?xml'))" disable-output-escaping="yes"/>

However, disable-output-escaping is an instruction to the serializer (it's the serializer that performs escaping, therefore if there is no serializer, there is nothing to disable). In this case there is no serializer, because you are sending the transformation output to an XdmDestination. So disable-output-escaping is ignored, and you therefore end up with the < character escaped as &lt;

You could make this work if the output were sent to a serializer. However, you would still have to be very careful, because the output would say encoding="iso-8859-1" in its XML declaration, and there is no guarantee that the output would actually be in this encoding.

The preferred way of handling this is to re-parse the XML contained in the CDATA section using the fn:parse-xml() function. Better still, avoid putting XML in CDATA sections in the first place - it's not what they were designed for.

Actions #3

Updated by Sunny boddu about 7 years ago

Hi Kay,

Thanks for your response,

I do not have any knowledge on how to use XSLT functions.

Can you please modify below function based on your suggestion using " fn:parse-xml()"

<xsl:value-of select="concat('<?xml',substring-after(//root/text(),'<?xml'))" disable-output-escaping="yes"/>

Thanks for your help

Actions #4

Updated by Michael Kay about 7 years ago

  • Assignee set to Michael Kay
  • Priority changed from High to Normal

I'm sorry, our support service doesn't extend to writing the code for you, or to providing training.

You might find StackOverflow useful as a resource - though the expectation there is that you show your best attempt and only ask for help when you are really stuck.

See if this gets you any further forward:

<xsl:copy-of select="parse-xml(//root/text())"/>

I would recommend putting version="3.0" in the stylesheet as parse-xml() is an XPath 3.0 function.

Actions #5

Updated by Sunny boddu about 7 years ago

Thanks a lot Kay. I will try to use that

Actions #6

Updated by Sunny boddu about 7 years ago

Hi Good Morning Kay,

This time I have changed the code using Serializer, It almost solved the problem that have with '&lt', '&gt'. Now I am getting proper xml.

But everytime the output xml contains the XML header ""

Is there any option for us to not to generate that xml header each and every time when we process them?

Option to omit the xml header generation? Can you please help us ?

Thanks for your time Kay.

XsltCompiler compiler = processor.NewXsltCompiler();

            XsltExecutable executable = compiler.Compile(new Uri(xsltPath));       

            XsltTransformer xslTrans = executable.Load();

            XdmNode input = processor.NewDocumentBuilder().Build(new Uri(xmlPath));

            xslTrans.InitialContextNode = input;

            Serializer serializer = processor.NewSerializer();

            serializer.SetOutputWriter(Console.Out);

            xslTrans.Run(serializer);
Actions #7

Updated by Sunny boddu about 7 years ago

sorry in previous post, xml header is missing which I was reporting about

But everytime the output xml contains the XML header ""

Actions #8

Updated by Sunny boddu about 7 years ago

I have figured that out,

By adding serializer.SetOutputProperty(Serializer.OMIT_XML_DECLARATION, "yes"), I can able to omit the declaration.

I am good Key.

Thank you

Actions #9

Updated by Michael Kay about 7 years ago

  • Status changed from New to Resolved

Glad you solved it. Marking as closed.

Actions #10

Updated by Sunny boddu about 7 years ago

Thansk Kay

Actions #11

Updated by Michael Kay almost 7 years ago

  • Status changed from Resolved to Closed

Please register to edit this issue

Also available in: Atom PDF