2011-08-20 22 views
17

Mam plik XML i zewnętrzny plik XSLT.Używanie wbudowanego pliku XSLT dla pliku XML

Obecnie w moim XML odsyłam do zewnętrznego linku XSLT przy użyciu href:

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

Jak mogę użyć inline XSLT zamiast? czy to możliwe? Jeśli tak, w jaki sposób?

Odpowiedz

10

Tak, możliwe jest osadzenie XSLT wewnątrz kodu XML.

XSLT to plik XML, więc po prostu trzeba upewnić się, że można umieścić go wewnątrz elementu dokumentu pliku XML, tak, że plik XML jest nadal dobrze uformowane.

W rzeczywistości it is described in the XSLT specification:

2.7 Embedding Stylesheets

Normalnie stylów XSLT jest kompletnym dokumentem XML z elementem xsl: stylesheet jako element dokumentu. Jednak arkusz stylów XSLT może być również osadzony w innym zasobie. Możliwe są dwie formy osadzania:

  • arkusza stylów XSLT można tekstowo wbudowane w nie-XML zasobów lub
  • Element xsl: stylesheet mogą wystąpić w dokumencie XML inny niż jako dokumentu element.

Aby ułatwić drugą formę osadzania, element xsl: stylesheet element może mieć atrybut ID, który określa unikalny identyfikator.

UWAGA: W celu takiego atrybutu do użycia z funkcją id XPath , to musi być rzeczywiście zadeklarowana w DTD jako identyfikator.

Poniższy przykład pokazuje, w jaki sposób można użyć instrukcji Xml-stylesheet do przetwarzania [Arkusz stylów XML], aby umożliwić dokumentowi zawieszenie własnego arkusza stylów. Odwołanie URI używa względną URI z identyfikator fragmentu zlokalizować elementem 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> 

UWAGA: arkusza stylów, jest osadzony w dokumencie, do którego jest być zastosowany lub taki, który może być zawarty lub zaimportowany do arkusza stylów, który jest tak osadzony, musi zawierać regułę szablonu, która określa, że ​​elementy xsl: stylesheet mają być ignorowane.

W zależności od tego, w jaki sposób zamierzasz go wykorzystać, osadzone arkusze stylów mogą nie być obsługiwane. Na przykład w IE 6/7/8.There are some workarounds.

0

Do testowania procesorów drugiej stronie klienta, używanie 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>