2015-09-25 50 views
0

我在XSLT全新的。所以我试图做一个应该输出元素值的例子。输出格式是纯文本格式。 这是XML文件名为cdcatalog.xml:为什么这个代码不输出任何XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?> 
<catalog> 
    <cd> 
     <title>Empire</title> 
     <artist>Bob Dylan</artist> 
     <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> 
      <europe_country>Bulgaria</europe_country> 
      <azia_coutry>China</azia_coutry> 
     </country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
</catalog> 

这是cdcatalog.xsl称为XSL文件

<?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:for-each select="cd"> 
      <xsl:value-of select="title"/> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我期望的输出是这样的

Empire 
Hide your heart 

但是输出窗口中没有任何内容。 我错在哪里? 预先感谢您。

回答

1

在编写模板,尝试目标(即比赛)直接与你所感兴趣的内容(title元素,在这种情况下)。

XSLT样式表

正如你所看到的,是匹配text()第二个模板。如果我们忽略它,从输入文档中的所有文本内容输出,因为那是XSLT处理器文本节点的默认行为

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

    <xsl:template match="title"> 
     <xsl:value-of select="."/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    <xsl:template match="text()"/> 

</xsl:transform> 

文本输出

Empire 
Hide your heart 

尝试这种解决方案的在线here


顺便说一句,请确保您的元素一贯命名。称为azia_coutry一个元素是从asia_coutryasia_country完全不同。

相关问题