2013-07-19 80 views
0

我試圖用XSLT將XML歌詞歌詞數據庫轉換爲HTML。我的XML是這樣開始的:XSLT:匹配名稱空間不起作用的元素

<?xml version='1.0' encoding='utf-8'?> 
<song xmlns="http://openlyrics.info/namespace/2009/song" version="0.8" createdIn="OpenLP  2.0.1" modifiedIn="OpenLP 2.0.1" modifiedDate="2012-03-14T02:21:52"> 
    <properties> 
    <titles> 
     <title>Amazing Grace</title> 
    </titles> 
    <authors> 
     <author>John Newton</author> 
    </authors> 
    </properties> 
    <lyrics> 
    <verse name="v1"> 
     <lines>Amazing grace, how sweet the sound<br/>That saved a wretch like me<br/>I once was lost, but now am found<br/>Was blind but now I see</lines> 

我從我需要上述命名空間添加到我的XSLT其他職位知道。我試過了,我的XSLT看起來像這樣:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:lyrics="http://openlyrics.info/namespace/2009/song"> 

<xsl:template match="lyrics:song"> 
    <h3><xsl:value-of select="properties/titles/title"/></h3> 
    <h4><xsl:value-of select="properties/authors/author"/></h4> 
    <xsl:for-each select="lyrics/verse"> 
    <xsl:copy-of select="lines" /><br /><br /> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

不幸的是,這段代碼仍然不起作用。我的XSLT只適用於當我消除XML中的xmlns條目並直接匹配「歌曲」時。任何人都可以指出我做錯了關於我標題爲「歌詞?」的xmlns命名空間嗎?我在XML/XSLT方面很新穎。提前致謝!

回答

1

您需要使用的任何元素名稱的命名空間前綴「歌詞」,這屬於命名空間http://openlyrics.info/namespace/2009/song」。這是所有元素名稱,因爲它是默認的名稱空間。試試像這樣(未測試):

<xsl:template match="lyrics:song"> 
    <h3><xsl:value-of select="lyrics:properties/lyrics:titles/lyrics:title"/></h3> 
    <h4><xsl:value-of select="lyrics:properties/lyrics:authors/lyrics:author"/></h4> 
    <xsl:for-each select="lyrics:lyrics/lyrics:verse"> 
    <xsl:copy-of select="lyrics:lines" /><br /><br /> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
+0

謝謝,我會在稍後嘗試。命名空間信息與我無關,那麼是否有任何方法使用XSLT消除XML開頭的標記中的xmlns信息? – brohrig

+0

刪除名稱空間聲明的最簡單方法是使用編輯器。用xlst做這件事看看例子:http://stackoverflow.com/a/6253642/2115381 –