XSLT:匹配元素与命名空间不起作用

时间:2013-07-19 05:08:33

标签: xml xslt namespaces match xml-namespaces

我正在尝试使用XSLT将XML歌词数据库转换为HTML。我的XML开头是这样的:

<?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条目并直接匹配“song”时才有效。任何人都可以指出我在xmlns命名空间方面做错了什么我称之为“歌词”?我是XML / XSLT的新手。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在属于命名空间http://openlyrics.info/namespace/2009/song”的任何元素名称处使用命名空间前缀“lyrics”。这都是元素名称,因为它是默认名称空间。尝试这样的somenthing(未经测试):

<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>
相关问题