3

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ranta
  • 785
  • 6
  • 6
  • 4
    I really wish people that downvote questions would give a reason. This site is as much about helping people to learn as anything else, and people can't learn if there's no feedback other than a downvote. – David Aug 25 '11 at 21:36
  • Is there another way to do what? What are you required to do: process the XML using the stylesheet specified in the `` processing instruction? Then yes you have to retrieve the pseudo-attribute from the PI. Or is your requirement to process it using some other predetermined stylesheet? – LarsH Aug 25 '11 at 21:54
  • BTW, please don't add " (C#)" to your title just to categorize your question. Tags do that better. – John Saunders Aug 26 '11 at 02:06

3 Answers3

5

<? ... ?> 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
Achim
  • 15,415
  • 15
  • 80
  • 144
  • 1
    I'm glad someone who knows what they are talking about helped this guy out. Good answer! +1. – David Aug 25 '11 at 21:53
  • 1
    This answer would be more helpful if you specified *how* to retrieve the href. Since PI's don't have true attributes, what's a good way to retrieve the value of `href` pseudo-attribute? E.g. http://stackoverflow.com/questions/2119699/how-can-i-get-the-href-attribute-value-out-of-an-xml-stylesheet-node – LarsH Aug 25 '11 at 21:56
  • See this question on how to retrieve the PI attribute: http://stackoverflow.com/questions/3100345/how-to-read-read-processing-instruction-from-an-xml-file-using-net-3-5 – Achim Aug 25 '11 at 21:59
2

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);
vittore
  • 17,449
  • 6
  • 44
  • 82
  • I don't see how these examples address the question about whether to get the name of the stylesheet from the processing-instruction. – LarsH Aug 25 '11 at 21:59
1

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.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218