How to work with embedded XML and XSL file

I am trying to create an embedded file containing both XML and XSL. The test is based on "XML and XSL in one file" at dpawson.co.uk. The source is as follows:

<?xml-stylesheet type="text/xml" href="#stylesheet"?> <!DOCTYPE doc [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]> <doc> <xsl:stylesheet id="stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- any xsl:import elements --> <xsl:template match="xsl:stylesheet" /> <!-- rest of your stylesheet --> </xsl:stylesheet> <!-- rest of your XML document --> </doc> 

I originally created a working XML and XSL file. XML looks like this:

 <?xml-stylesheet type="text/xsl" href="data.xsl"?> <Report> <ReportFor>Test Data</ReportFor> <CreationTime>2009-07-29 05:37:14</CreationTime> </Report> 

And the data.xsl file looks like this:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <!-- ... --> <xsl:value-of select="Report/ReportFor" /> <!-- ... --> <xsl:value-of select="Report/CreationTime"/> <!-- ... --> </xsl:template> </xsl:stylesheet> 

Based on this, I am trying to create an embedded XML file containing both XML and XSL.

Currently, this file is as follows:

 <?xml-stylesheet type="text/xsl" href="#stylesheet"?> <!DOCTYPE doc [ <!ATTLIST xsl:stylesheet id ID #REQUIRED> ]> <doc> <xsl:stylesheet id="stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- any xsl:import elements --> <xsl:template match="xsl:stylesheet" /> <!-- rest of your stylesheet --> <xsl:template match="/"> <!-- ... --> <xsl:value-of select="Report/ReportFor" /> <!-- ... --> <xsl:value-of select="Report/CreationTime"/> <!-- ... --> </xsl:template> </xsl:stylesheet> <!-- rest of your XML document --> <Report> <ReportFor>Test Data</ReportFor> <CreationTime>2009-07-29 05:37:14</CreationTime> </Report> </doc> 

The problem with this document is that <xsl:value-of> does not retrieve the data represented in the XML section. How can <xsl:value-of> recognize embedded data? Is any special syntax required?

+6
xml xslt
source share
2 answers

You are missing the doc element in the template matching attribute. Try instead:

 <xsl:template match="/doc"> <xsl:value-of select="Report/ReportFor" /> </xsl:template> 
+1
source share

Do I need to use "." operator to get the value?

  <xsl:value-of select="Report/ReportFor/."/> 
0
source share

All Articles