2011-08-20 9 views
17

Ho un file XML e un file XSLT esterno.Utilizzo di XSLT in linea per un file XML

Attualmente, all'interno del mio XML mi riferisco a un collegamento XSLT esterno utilizzando un href:

<?xml version="1.0" encoding="utf-8"?> 
    <?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?> 
    <mytag> 
     <t1> </t1> 
     <t2> </t2> 
     <t3> <t3> 
    <mytag> 

Come posso usare inline XSLT, invece? È possibile? Se sì, come?

risposta

10

Sì, è possibile incorporare l'XSLT all'interno dell'XML.

XSLT è un file XML, quindi si sarebbe solo bisogno di fare in modo che si inserisce all'interno del documento elemento del file XML, in modo che il file XML è ancora ben formato.

Infatti, it is described in the XSLT specification:

2.7 Embedding Stylesheets

normalmente un foglio di stile XSLT è un documento XML completo con la xsl: stylesheet come elemento documento. Tuttavia, un foglio di stile XSLT può anche essere incorporato in un'altra risorsa. Due forme di incorporamento sono possibili:

  • il foglio di stile XSLT può essere testualmente incorporato in un non-XML risorsa o
  • l'elemento xsl: stylesheet possono verificarsi in un documento XML diverso come il documento elemento.

Per facilitare la seconda forma di incorporamento, xsl: stylesheet è consentito avere un attributo ID che specifica un identificatore univoco.

NOTA: Affinché tale attributo da utilizzare con la funzione XPath id , si deve effettivamente essere dichiarato nella DTD come un ID.

Il seguente esempio mostra come il foglio di stile xml che elabora l'istruzione [Foglio di stile XML] può essere utilizzato per consentire a un documento di di contenere il proprio foglio di stile. Il riferimento URI utilizza un URI relativo con di frammento per localizzare l'elemento xsl: stylesheet:

<?xml-stylesheet type="text/xml" href="#style1"?> 
<!DOCTYPE doc SYSTEM "doc.dtd"> 
<doc> 
<head> 
<xsl:stylesheet id="style1" 
       version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:import href="doc.xsl"/> 
<xsl:template match="id('foo')"> 
    <fo:block font-weight="bold"><xsl:apply-templates/></fo:block> 
</xsl:template> 
<xsl:template match="xsl:stylesheet"> 
    <!-- ignore --> 
</xsl:template> 
</xsl:stylesheet> 
</head> 
<body> 
<para id="foo"> 
... 
</para> 
</body> 
</doc> 

NOTA: Un foglio di stile incorporato nel documento di cui deve essere applicato o che può essere incluso o importato in un foglio di stile che è così incorporato in genere deve contenere una regola modello che specifica che xsl: gli elementi del foglio di stile devono essere ignorati.

A seconda di come si prevede di sfruttarlo, i fogli di stile incorporati potrebbero non essere supportati. Ad esempio, in IE 6/7/8.There are some workarounds.

0

Per la prova di tutti i processori lato client, utilizzare un self-referencing stylesheet:

<?xml version="1.0" encoding="utf-8"?> 
<!--Reference the file name as the href value--> 
<?xml-stylesheet type="text/xsl" href="html5.xml"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" 
       > 

<!-- Output HTML doctype with text/html content-type and without XML declaration--> 
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="yes" doctype-system="about:legacy-compat" /> 


<!-- Read the children of the stylesheet itself --> 
<xsl:template match="xsl:stylesheet"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- Output the HTML markup--> 
<xsl:template match="/"> 
    <html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
     <link rel="stylesheet" type="text/css" href="foo.css"/> 
    </head> 
    <body> 
     <div class="foo"> 
     <span class="bar"> 
      <span class="baz">1</span> 
     </span> 
     <!--Added comment to fill empty node--> 
     <span class="placeholder"><xsl:comment/></span> 
     </div> 

     <!-- Read matching templates --> 
     <xsl:apply-templates /> 
     <!--Add comment to fill empty script tag--> 
     <script src="foo.js" type="application/x-javascript"><xsl:comment/></script> 
    </body> 
    </html> 
</xsl:template> 

<!-- Don't reprint text nodes within the xsl:stylesheet node --> 
<xsl:template match="text()"/> 

<!-- Read non-namespaced nodes within the xsl:stylesheet node --> 
<xsl:template match="//node()[local-name() = name()]"> 
    <xsl:if test="local-name() = 'foo'"> 
    <xsl:variable name="foo" select="."/> 

    <input type="text" id="{$foo}" value="{$foo}"></input> 
    </xsl:if> 
    <xsl:apply-templates/> 
</xsl:template> 

<test> 
<foo>A</foo> 
<foo>B</foo> 
<foo>C</foo> 
</test> 

</xsl:stylesheet> 
Problemi correlati