Modify XSLT using C# Code - c#

I am Working on Visual-studio 2012 in C#.
I want to update the value of a node of a XSLT.
This abc.xslt is like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<DocumentElement>
<PositionMaster>
<Name>
<xsl:value-of select = "'Ryan'"/>
</Name>
</PositionMaster>
</DocumentElement>
Code i have written to modify this XSLT in the C# is:
XmlDocument xslDoc = new XmlDocument();
xslDoc.Load(abc.xslt);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
I am looking to change the value of Name field to David. What should i write further here?

XmlElement valueOf = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:template[#match = '/']/DocumentElement/PositionMaster/Name/xsl:value-of", nsMgr);
if (valueOf != null)
{
valueOf.SetAttribute("select", "'David'");
xslDoc.Save("new.xslt");
}
else
{
// handle case here that element was not found
}

You seem to be going about this a very odd way. Why not just use a stylesheet parameter (a global xsl:param element)?
And if you do need to modify a source stylesheet, as you sometimes do, surely it makes more sense to use XSLT for the purpose?

Related

Saxon XSLT: Serializer producing weird indents

I'm using Saxon HE 9.5.1.8 to transform an XML to another XML file.
My problem is that the XML content written by the Serializer() class of Saxon prints out several additional indents that I don't want to have in there. I'm assuming that this is "wrong" because I got the expected output when using the DomDestination() class (but then the outer XML document information is missing) or other XSL transformers like the one that is shipped with Visual Studio / .NET Framework.
This is the input XML:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>$44.95</price>
<publish_date>2000-10-01</publish_date>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>$5.95</price>
<publish_date>2000-12-16</publish_date>
</book>
This is the XLST file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:copy-of select="#*|book/#*" />
<xsl:for-each select="*">
<xsl:attribute name="{name()}">
<xsl:value-of select="text()"/>
</xsl:attribute>
</xsl:for-each>
</book>
</xsl:template>
</xsl:stylesheet>
That is the expected output:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101" author="Gambardella, Matthew" title="XML Developer's Guide" genre="Computer" price="$44.95" publish_date="2000-10-01" />
<book id="bk102" author="Ralls, Kim" title="Midnight Rain" genre="Fantasy" price="$5.95" publish_date="2000-12-16" />
</catalog>
And that is the output when using Saxon:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101"
author="Gambardella, Matthew"
title="XML Developer's Guide"
genre="Computer"
price="$44.95"
publish_date="2000-10-01"/>
<book id="bk102"
author="Ralls, Kim"
title="Midnight Rain"
genre="Fantasy"
price="$5.95"
publish_date="2000-12-16"/>
</catalog>
Does anybody know how to suppress or modify this behavior of Saxon? That is the C# code that is used to call the Saxon API:
public Stream Transform(string xmlFilePath, string xsltFilePath)
{
var result = new MemoryStream();
var xslt = new FileInfo(xsltFilePath);
var input = new FileInfo(xmlFilePath);
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new Uri(xslt.FullName));
var destination = new Serializer();
destination.SetOutputStream(result);
using(var inputStream = input.OpenRead())
{
var transformer = executable.Load();
transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
transformer.Run(destination);
}
result.Position = 0;
return result;
}
Try setting http://saxonica.com/documentation9.5/extensions/output-extras/line-length.html to a very large value to avoid that attributes are put on a new line: <xsl:output xmlns:saxon="http://saxon.sf.net/" saxon:line-length="1000"/>.
Your goal of having multiple processors produce output in the same format is hopelessly misguided. That's especially so if you choose indented output: the spec leaves it entirely to implementations how to do indentation, saying only that the goal is to make it human-readable. (And placing constraints on where extra whitespace can be inserted.)
I'm sorry you don't find Saxon's way of wrapping long attribute lists pleasing, but it is entirely within the letter and the spirit of the specification. Without it, if you have an element with eight namespace declarations, you can easily get a line that is 400 characters long, which I certainly don't regard as human-readable.
There are many reasons that comparing two XML documents lexically is never going to work. For example, the attributes can be in a different order. There are two ways of comparing XML: convert the documents into canonical form using a "Canonical XML" processor, or compare them at the tree level for example by using the XPath 2.0 deep-equal() function. Ideally (especially if you want to know where the differences are, rather than just whether differences exist), use a specialist XML comparison tool such as DeltaXML.
For what it's worth, when we do unit testing, we first attempt a lexical comparison of the results. If that fails, we parse both documents and compare them using saxon:deep-equal(), which is a modified form of the deep-equal() function that gives fine control over the comparison rules, e.g. handling of whitespace and handling of namespaces.

How to get an absolute path to the directory of the XSL file?

My Schema.xsd file is located in the same directory with the .xsl file. In the .xsl file I would like to generate a link to Schema.xsl in the generated output. The generated output is located in different directories. Currently I do it like this:
<xsl:template match="/">
<root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../Schema.xsd">
<!-- . . . -->
However this forces the generated output to be located 3 levels under the directory of Schema.xsd. I would like to generate an absolute path to the schema in the output, so the output could be located anywhere.
Update. I use XSLT 1.0 (XslCompiledTransform implementation in .NET Framework 4.5).
XSLT 2.0 Solution
Use the XPath 2.0 function, resolve-uri():
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"
omit-xml-declaration="yes"
encoding="UTF-8"/>
<xsl:template match="/">
<root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="{concat(resolve-uri('.'), 'Schema.xsd')}">
</root>
</xsl:template>
</xsl:stylesheet>
Yields, without parameter passing and regardless of the input XML:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0"
xsi:noNamespaceSchemaLocation="file:/c:/path/to/XSLT/file/Schema.xsd"/>
This is a sketch of how to do it (also see Passing parameters to XSLT Stylesheet via .NET).
In your C# code you need to define and use a parameter list:
XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("SchemaLocation","","<SOME_PATH_TO_XSD_FILE>");
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("<SOME_PATH_TO_XSLT_FILE>");
using (StreamWriter sw = new StreamWriter("<SOME_PATH_TO_OUTPUT_XML>"))
{
transform.Transform("<SOME_PATH_TO_INPUT_XML>", argsList, sw);
}
Your XSLT could be enhanced like this:
...
<xsl:param name="SchemaLocation"/> <!-- this more or less at the top of your XSLT! -->
...
<xsl:template match="/">
<root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="{$SchemaLocation}">
...
...
</xsl:template>
....

Remove all CDATA nodes and replace with encoded text

So, I've got a massive XML file and I want to remove all CDATA sections and replace the CDATA node contents with safe, html encoded text nodes.
Just stripping out the CDATA with a regex will of course break the parsing. Is there a LINQ or XmlDocument or XmlTextWriter technique to swap out the CDATA with encoded text?
I'm not too concerned with the final encoding quite yet, just how to replace the sections with the encoding of my choice.
Original Example
---
<COLLECTION type="presentation" autoplay="false">
<TITLE><![CDATA[Rights & Responsibilities]]></TITLE>
<ITEM id="2802725d-dbac-e011-bcd6-005056af18ff" presenterGender="male">
<TITLE><![CDATA[Watch the demo]]></TITLE>
<LINK><![CDATA[_assets/2302725d-dbac-e011-bcd6-005056af18ff/presentation/presentation-00000000.mp4]]></LINK>
</ITEM>
</COLLECTION>
---
Sould Become
<COLLECTION type="presentation" autoplay="false">
<TITLE>Rights & Responsibilities</TITLE>
<ITEM id="2802725d-dbac-e011-bcd6-005056af18ff" presenterGender="male">
<TITLE>Watch the demo</TITLE>
<LINK>_assets/2302725d-dbac-e011-bcd6-005056af18ff/presentation/presentation-00000000.mp4</LINK>
</ITEM>
</COLLECTION>
I guess the ultimate goal is to move to JSON. I've tried this
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath( #"~/somefile.xml"));
string jsonText = JsonConvert.SerializeXmlNode(doc);
But I end up with ugly nodes, i.e. "#cdata-section" keys. It would take WAAAAY to many hours to have the front end re-developed to accept this.
"COLLECTION":[{"#type":"whitepaper","TITLE":{"#cdata-section":"SUPPORTING DOCUMENTS"}},{"#type":"presentation","#autoplay":"false","TITLE":{"#cdata-section":"Demo Presentation"},"ITEM":{"#id":"2802725d-dbac-e011-bcd6-005056af18ff","#presenterGender":"male","TITLE":{"#cdata-section":"Watch the demo"},"LINK":{"#cdata-section":"_assets/2302725d-dbac-e011-bcd6-005056af18ff/presentation/presentation-00000000.mp4"}
Process the XML with a XSLT that just copies input to output - C# code:
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(#"c:\temp\id.xslt");
transform.Transform(#"c:\temp\cdata.xml", #"c:\temp\clean.xml");
id.xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Using LINQ to XML, you can do it like this:
XDocument doc = …;
var cDataNodes = doc.DescendantNodes().OfType<XCData>().ToArray();
foreach (var cDataNode in cDataNodes)
cDataNode.ReplaceWith(new XText(cDataNode));
I think you can load the xml into a XmlDocument class. Then recursively process each XmlNode and look for XmlCDataSection node. This XmlCDataSection node should be replaced withXmlTextNode with same value.

Using C# to do xslt transform ignoring xsl:output

I am doing an xslt transform inside my c# program. When I run the xslt on its own it outputs just fine, but when I run it from within my c# program it always leaves off the:
<?xml version="1.0" encoding="UTF-8"?>
At the top of the resulting xml document. My XSLT file looks like:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:hd="http://www.hotdocs.com/schemas/component_library/2009"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xml="http://www.w3.org/XML/1998/namespace">
<xsl:output method="xml" omit-xml-declaration="no" version="1.0" encoding="UTF-8"/>
<xsl:template match="/xsd:schema">
<hd:componentLibrary xmlns:hd="something" version="10">
</hd:componentLibrary>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
I am running the xslt in my c# program like this:
XPathDocument myXPathDoc = new XPathDocument(PathToXMLDocument);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(PathToXSLTDocument);
XmlTextWriter myWriter = new XmlTextWriter(PathToOutputLocation, null);
myXslTrans.Transform(myXPathDoc,null,myWriter);
myWriter.Close();
I have tried the xslt document without the xsl:output line, but that does not seem to help.
How can i get the ?xml tag at the top of my outputted xml file?
Thanks
XmlTextWriter is a bit outdated. I recommend you switch to XmlWriter.Create.
Then you can specify OmitXmlDeclaration = false in the XmlWriterSettings.
If you use XmlWriter.Create() then you can pass an XmlWriterSettings instance as a parameter. The OmitXmlDeclaration member in the settings class controls whether or not the tag is included.

How can I render custom XSL controls in my XSLT file with C#?

I'm using C# to translate a XML file to HTML with the use of XSLT.
I use an Extension object to render my own code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:widget="urn:serverTime"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="demo:printTime()"/>
</xsl:template>
and in my C#:
XsltArgumentList myList = new XsltArgumentList();
myList.AddExtensionObject("demo:serverTime", new ServerTime());
transform.Transform(document, myList, writer);
This works perfectly. However, I would like to create my own custom tags like:
<demo:printTime />
This doesn't work: the tag is printed to the output without being rendered. How can I make this work so I can use my own tags?
You can't do this. XSLT does not support "custom tags".
If you want to print out anything that is not a literal value, then it must be the result of a function call, wrapped in <xsl:value-of/>.

Categories