2016-08-30 10 views
0

XSLファイルをHTML形式で生成しようとしましたが、動作しません。私は問題は私が定義したテンプレートマッチから来ると思う。ここでXSL変換でのテンプレートマッチの定義方法は?

は、私は、テンプレートの一致を内蔵したものとXMLである:ここでは

<?xml version="1.0" encoding="ISSO−8859−15" ?> 
<Library> 
    <authors> 
    <Author id="author1" name="Einstein"/> 
    </authors> 
    <publications> 
    <Book year="1900" title="7 Kingdoms" author="author1"/> 
    <Magazine year="2010" number="203" title="The News"'/> 
    <Book year="1956" title="The Fall" author="author1"/> 
    </publications> 
</Library> 

は私がディスプレイ作家、書籍や出版物に取得したいHTML形式です:

<html> 
    <h1>Library</h1> 
    <h2>Authors</h2> 
    <ul> 
     <li id="author1">Einstein</li> 
    </ul> 
    <h2>Books</h2> 
    <ul> 
     <li>7 Kingdoms, 1942, <a href="#author">Einstein</a></li> 
     <li>The Fall, 1956, <a href="#author">Einstein</a></li> 
    </ul> 
    <h2>Magazines</h2> <ul> 
     <li>The News (203), 2010</li> 
    </ul> 
</html> 

これは、私が作成したテンプレートマッチです:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html"/> 

<xsl:template match="Author"> 
    <li id= "library/author/Author/@id"> 
     <xsl:value-of select="library/authors/Author/name"/>  
    </li> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="Magazines"> 
    <li> 
     <xsl:value-of select="library/publications/magazine[@title]"/> 
     (
     <xsl:value-of select="library/publications/magazine[@number]"/> 
     ), 
     <xsl:value-of select="library/publications/magazine[@year]"/> 

    </li> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="Book"> 
    <li> 
     <xsl:value-of select="library/publications/book[@year]"/> 
     , 
     <xsl:value-of select="library/publications/book[@title]"/> 
     , 
     <xsl:value-of select="library/publications/book/author/@ref"/> 
    </li> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 

前述のように、HTMLファイルを適切に埋め込むことができないテンプレートマッチから問題が生じると思います。

ご協力いただければ幸いです。

+0

変換に必要なロジックの説明に加えて、** **に加えて、**の例があります。**ではなく**です。 –

+0

library/publications/bookのような相対パス式は、コンテキスト項目*で始まる選択、つまりテンプレートルールが一致する要素であることを理解する必要があります。すべての選択がルートから始まっているように見えます。 –

答えて

0

私の知る限り推測できるように、あなたがしたい(!):あなたの入力例に適用

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 

<xsl:key name="author" match="Author" use="@id" /> 

<xsl:template match="/Library"> 
    <html> 
     <h1>Library</h1> 
     <h2>Authors</h2> 
     <ul> 
      <xsl:apply-templates select="authors/Author"/> 
     </ul> 
     <h2>Books</h2> 
     <ul> 
      <xsl:apply-templates select="publications/Book"/> 
     </ul> 
     <h2>Magazines</h2> 
     <ul> 
      <xsl:apply-templates select="publications/Magazine"/> 
     </ul> 
    </html> 
</xsl:template> 

<xsl:template match="Author"> 
    <li id="{@id}"> 
     <xsl:value-of select="@name"/>  
    </li> 
</xsl:template> 

<xsl:template match="Book"> 
    <li> 
     <xsl:value-of select="@title"/> 
     <xsl:text>, </xsl:text> 
     <xsl:value-of select="@year"/>  
     <xsl:text>, </xsl:text> 
     <a href="#author"> 
      <xsl:value-of select="key('author', @author)/@name"/> 
     </a> 
    </li> 
</xsl:template> 

<xsl:template match="Magazine"> 
    <li> 
     <xsl:value-of select="@title"/> 
     <xsl:text> (</xsl:text> 
     <xsl:value-of select="@number"/> 
     <xsl:text>), </xsl:text> 
     <xsl:value-of select="@year"/> 
    </li> 
</xsl:template> 

</xsl:stylesheet> 

(ライン#で余分なアポストロフィを除去した後7)

<html> 
    <h1>Library</h1> 
    <h2>Authors</h2> 
    <ul> 
     <li id="author1">Einstein</li> 
    </ul> 
    <h2>Books</h2> 
    <ul> 
     <li>7 Kingdoms, 1900, <a href="#author">Einstein</a></li> 
     <li>The Fall, 1956, <a href="#author">Einstein</a></li> 
    </ul> 
    <h2>Magazines</h2> 
    <ul> 
     <li>The News(203), 2010</li> 
    </ul> 
</html> 
関連する問題