Support #3186
closedSaxon API: How to extract output from XdmDestination
0%
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>
<connections>
<conn-name>qa-gdc1</conn-name>
</connections>
</svs>
</show>
</nf:data>
</nf:rpc-reply>
Files
Updated by Sunny boddu over 7 years ago
- File output.xml output.xml added
- File output.PNG output.PNG added
The actual output which I am having is not showing properly in this patch. Please refer attached output.xml .
'<' and '>' are still showing in output xml..... It is not showing as '<', '>'
Updated by Michael Kay over 7 years ago
You are attempting to write lexical XML with the code:
<xsl:value-of select="concat('<?xml',substring-after(//root/text(),'<?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 <
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.
Updated by Sunny boddu over 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
Updated by Michael Kay over 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.
Updated by Sunny boddu over 7 years ago
Thanks a lot Kay. I will try to use that
Updated by Sunny boddu over 7 years ago
Hi Good Morning Kay,
This time I have changed the code using Serializer, It almost solved the problem that have with '<', '>'. 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);
Updated by Sunny boddu over 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 ""
Updated by Sunny boddu over 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
Updated by Michael Kay over 7 years ago
- Status changed from New to Resolved
Glad you solved it. Marking as closed.
Please register to edit this issue