Ranta
Ranta

Reputation: 815

XslTransform with xml-stylesheet

It looks like everyone says if you use XslTransform, you would call Load to load the style sheet first, then call Transform to transform it. However I have the following XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="some_stylesheet.xsl" ?>
....
</xml>

Am I suppose to load the xml first, find the style sheet node, then call Load to load the style sheet, or is there another way to do it?

Upvotes: 3

Views: 1401

Answers (3)

vittore
vittore

Reputation: 17579

You are loading source xml, loading xslt and apply one to another There is limited native XSLT2 support in .NET so i recomend using AltovaXML library altova xml, usage can be found there altova xml online documentaion

XSLT 2.0 transformation (XML to XML)

// Specify folder (AltovaXMLExamples folder)
// Check if filepath is correct for you
String strExamplesFolder = Environment.GetEnvironmentVariable("ProgramFiles") +   
"\\Altova\\AltovaXML2011\\AltovaXMLExamples\\";

// Create a new AltovaXML instance and access its engines
Altova.AltovaXML.Application AltovaXML = new Altova.AltovaXML.Application();

// Use XSLT2 Engine of AltovaXML to transform simple.xml using CopyInputXSLT2.xsl
Altova.AltovaXML.IXSLT2 AltovaXMLXSLT2 = AltovaXML.XSLT2;
 AltovaXMLXSLT2.InputXMLFileName = strExamplesFolder + "simple.xml";
 AltovaXMLXSLT2.XSLFileName = strExamplesFolder + "CopyInputXSLT2.xsl";
AltovaXMLXSLT2.Execute(strExamplesFolder + "simpleOutputFromXML.xml");

XSLT 2.0 transformation (String to XML)

// Specify folder (AltovaXMLExamples folder)
// Check if filepath is correct for you
String strExamplesFolder = Environment.GetEnvironmentVariable("ProgramFiles") + 
    "\\Altova\\AltovaXML2011\\AltovaXMLExamples\\";

// Create a new AltovaXML instance and access its engines
Altova.AltovaXML.Application AltovaXML = new Altova.AltovaXML.Application();

// Use XSLT2 Engine of AltovaXML to transform input string using CopyInputXSLT2.xsl
Altova.AltovaXML.IXSLT2 AltovaXMLXSLT2 = AltovaXML.XSLT2;
 AltovaXMLXSLT2.InputXMLFromText = "<?xml version='1.0'?><doc>Hello World</doc>";
 AltovaXMLXSLT2.XSLFileName = strExamplesFolder + "CopyInputXSLT2.xsl";
AltovaXMLXSLT2.Execute(strExamplesFolder + "simpleOutputFromString.xml");

XSLT 2.0 transformation (String to String)

// Specify folder (AltovaXMLExamples folder)
// Check if filepath is correct for you
String strExamplesFolder = Environment.GetEnvironmentVariable("ProgramFiles") +     
    "\\Altova\\AltovaXML2011\\AltovaXMLExamples\\";

// Create a new AltovaXML instance and access its engines
Altova.AltovaXML.Application AltovaXML = new Altova.AltovaXML.Application();

// Use XSLT2 Engine of AltovaXML to transform input string using CopyInputXSLT2.xsl
Altova.AltovaXML.IXSLT2 AltovaXMLXSLT2 = AltovaXML.XSLT2;
AltovaXMLXSLT2.InputXMLFromText = "<?xml version='1.0'?><doc>Hello World</doc>";
AltovaXMLXSLT2.XSLFileName = strExamplesFolder + "CopyInputXSLT2.xsl";
String strResult = AltovaXMLXSLT2.ExecuteAndGetResultAsString();

// Show result
MessageBox.Show("XSLT 2.0 engine answered: " + strResult);

Upvotes: 2

Robert Rossney
Robert Rossney

Reputation: 96740

Implementing the xml-stylesheet processing instruction is up to the user agent. The .NET XML library isn't a user agent, your program is. So yes, you're going to have to look at this processing instruction and load the XSLT yourself.

If you need to handle this PI in all of its glory, you should definitely look at the W3C recommendation. The PI can contain more than just a reference to a stylesheet (i.e. the pseudo-attributes). This may be important if the documents you're processing use every aspect of the PI.

Upvotes: 1

Achim
Achim

Reputation: 15702

<? ... ?> are processing instructions (PI), so <?xml-stylesheet ... ?> is a hint how to transform your xml. But that does not happen automatically. Interpretation of those instructions depend on the processor. What to do with this information depends on your requirements:

You could load your xml via XmlDocument, XDocument, ... and the PI will be ignored. You can do with your xml what ever you want: Use it as it is, transform it via any xsl transformation of your choice or retrieve the href-Attribute of the PI and use that transformation.

To come back to your original question: If you want your xml to be transformed by the given XSLT, then the workflow is as you expected it:

  • Load xml
  • Retrieve href to xslt transformation
  • Load xslt
  • Transform xml via xslt

Upvotes: 5

Related Questions