IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

Adding XSL Reference Options · View
VitalyT
Posted: Thursday, April 9, 2009 11:12:16 AM
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?

island
Posted: Friday, April 10, 2009 4:14:39 AM
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);
VitalyT
Posted: Friday, April 10, 2009 9:12:24 AM
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?

VitalyT
Posted: Friday, April 10, 2009 10:18:59 AM
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...
Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.