2016-08-30 39 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

您需要了解相對路徑表達式(如庫/出版物/書籍)從上下文項*開始進行選擇*,即模板規則匹配的元素。您的所有選擇看起來都是從根開始的。 –

回答

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>