2015-10-07 108 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 />,则应该获得所需的输出。

相关问题