|
|
Rank: Member
Joined: 4/9/2009 Posts: 11
|
We are using C# code generated from XML schema. And while it was very simple to add reference to an XML schema by calling SetSchemaLocation on the document object, I cannot figure out how to add a reference to XSL.
I'm trying to produce a document with the following header:
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="LangMaster.xsl"?> <LangMaster xsi:noNamespaceSchemaLocation="LangMaster.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Please, help me - how do I add that XSL reference using generated C# library?
|
|
Rank: Newbie
Joined: 10/28/2002 Posts: 1,283 Location: AT
|
Hi Vitaly,
to do this you can access the DOM directly, for example if I generate code based upon a schema "test.xsd":
<?xml version="1.0" encoding="UTF-8"?> <!-- edited with XMLSpy v2009 (https://www.altova.com) by chris luntzer (ASC X12) --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="a"> <xs:complexType> <xs:sequence> <xs:element name="b" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
the code to generate a processing instruction would be the following:
test.test2 doc = test.test2.CreateDocument(); test.aType root = doc.a.Append(); root.Node.OwnerDocument.InsertBefore(root.Node.OwnerDocument.CreateProcessingInstruction("xml-stylesheet", "LangMaster.xsl"), root.Node); doc.SaveToFile("test1.xml", true);
|
|
Rank: Member
Joined: 4/9/2009 Posts: 11
|
Thank you, island.
However, your code example produces the following instruction:
<?xml-stylesheet LangMaster.xsl?>
while I wanted precisely:
<?xml-stylesheet type="text/xsl" href="LangMaster.xsl"?>
What else do I need to do to achieve the desired results?
|
|
Rank: Member
Joined: 4/9/2009 Posts: 11
|
Ok,
After a long try an error, I found a way to make it work:
XmlProcessingInstruction ins = root.Node.OwnerDocument.CreateProcessingInstruction("xml-stylesheet", ""); ins.InnerText = "type=\"text/xsl\" href=\"LangMaster.xsl\" "; XmlNode node = root.Node.OwnerDocument.InsertBefore(ins, root.Node);
I don't know how valid this operation is, but it works for me, creating the following node: <?xml-stylesheet type="text/xsl" href="LangMaster.xsl"?>
Is there a better (more elegant) way of doing this? 'Cos this one seems a bit of hacking...
|
|
|
guest |