2011-08-20 17 views
17

私はXMLファイルと外部のXSLTファイルを持っています。XMLファイルにインラインXSLTを使用する

は現在、私のXML内私はHREFを使用して外部XSLTのリンクを参照してください。

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

は、どのように私の代わりに、インラインXSLTを使用することができますか?これは可能ですか?はいの場合、どうですか?

答えて

10

はい、XMLの内部にXSLTを埋め込むことは可能です。

XSLT は、XMLファイルなので、XMLファイルは、まだよく形成されるように、あなただけの、あなたがあなたのXMLファイルのドキュメント要素の中に置くことを確認する必要があります。実際に

it is described in the XSLT specification:ドキュメント要素としてstylesheet要素:

2.7 Embedding Stylesheets

通常、XSLTスタイルシートは XSLとの完全なXMLドキュメントです。ただし、XSLT スタイルシートを別のリソースに埋め込むこともできます。 XSL XSLTスタイルシートをテキストで非XML リソースに埋め込まれていてもよい

  • 、又は
  • : 埋め込みの2つの形式が可能であるstylesheetエレメントは、文書として 以外のXMLドキュメントで発生することが素子。

xsl:stylesheet要素 は、一意の識別子を指定するID属性を持つことができます。

注:このような属性をXPath id 関数で使用するには、実際にはDTDでIDとして宣言する必要があります。

次の例は、xml-stylesheet処理 命令[XML Stylesheet]を使用して、文書に に独自のスタイルシートを含める方法を示しています。 URI参照は、XSLを検索する フラグメント識別子と相対URIを使用する: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> 

注:それは にすることであるドキュメント内に埋め込まれているスタイルシートまたは が埋め込まれているスタイルシートに含まれているか、または取り込まれている可能性があります。通常、 にはxsl:stylesheet要素を無視するように指定するテンプレートルールが必要です。

これを活用する方法によっては、埋め込みスタイルシートがサポートされない場合があります。たとえば、IE 6/7/8の場合。There are some workarounds。クライアント側のプロセッサを横切って試験のため

0

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> 
関連する問題