2015-10-07 54 views
-1

我是XSLT新手,我不明白模板的基本概念。 我正在做一個實驗,看看輸出是否是我期望的。不幸的是,這是文件的轉換:爲什麼不輸出標籤的值

的XML:

<?xml version="1.0" encoding="UTF-8"?> 

<catalog> 
    <cd> 
     <title>Empire</title> 
     <artist>Bob Dylan</artist> 
     <continent>America</continent> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <year>1985</year> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>Bulgaria</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
</catalog> 

這是XSLT:

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="text"/> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="//continent"/> 
    </xsl:template> 
    <xsl:template match="//continent"> 
     <xsl:value-of select="continent"/> 
    </xsl:template> 
</xsl:stylesheet> 

輸出是空單。我期望打印出大陸標籤的價值,即America。請澄清一下這件事情。謝謝tou。

回答

0

錯誤就出在這裏:

<xsl:template match="//continent"> 
    <xsl:value-of select="continent"/> 
</xsl:template> 

在這個模板,你相匹配的<continent>,並與<xsl:value-of select="continent" />嘗試從另一<continent>子標籤(不存在)的信息。

如果您使用<xsl:value-of select="."/>,<xsl:value-of select="text()"/>或甚至是<xsl:apply-templates />,則應該獲得所需的輸出。

相關問題