2011-08-20 17 views
17

Tengo un archivo XML y un archivo XSLT externo.Uso de XSLT en línea para un archivo XML

En la actualidad, dentro de mi XML Me refiero a un enlace XSLT externo utilizando 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> 

¿Cómo puedo usar XSLT en línea en lugar? es posible? Si es así, ¿cómo?

Respuesta

10

Sí, es posible incrustar el interior de su XSLT XML.

XSLT es un archivo XML, por lo que sólo tendría que asegurarse de que lo pones en el interior del elemento de documentos de su archivo XML, por lo que todavía está bien formado en el archivo XML.

De hecho, it is described in the XSLT specification:

2.7 Embedding Stylesheets

Normalmente una hoja de estilo XSLT es un documento XML completo con el elemento xsl: hoja de estilos como el elemento de documento. Sin embargo, una hoja de estilo XSLT también se puede incrustar en otro recurso. Dos formas de incrustación son posibles:

  • la hoja de estilo XSLT puede incrustarse textualmente en un no-XML de recursos, o
  • el XSL: puede ocurrir en un documento XML que no sea como el documento elemento de hoja de estilo elemento.

Para facilitar la segunda forma de la inserción, el elemento xsl: hoja de estilo se le permite tener un atributo ID que especifica un identificador único.

NOTA : Para que tal atributo para ser utilizado con la función XPath Identificación , lo que realmente debe ser declarado en la DTD como una identificación.

El siguiente ejemplo muestra cómo la instrucción de procesamiento xml-stylesheet [hoja de estilo XML] se puede utilizar para permitir que un documento a contiene su propia hoja de estilo. La referencia URI utiliza un URI relativa con un identificador de fragmento para localizar el elemento xsl: hoja de estilo:

<?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: Una hoja de estilo que se incrusta en el documento al que se va a haber aplicado o que puede estar incluido o importarse en una hoja de estilo que está tan integrado, generalmente debe contener una regla de plantilla que especifica que los elementos xsl: stylesheet deben ignorarse.

Dependiendo de cómo piense aprovecharlo, es posible que las hojas de estilo incrustadas no sean compatibles. Por ejemplo, en IE 6/7/8.There are some workarounds.

0

Para las pruebas a través de los procesadores del lado del cliente, utilice 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> 
Cuestiones relacionadas