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

标签 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 条目并直接匹配“歌曲”时才有效。谁能指出我在名为“歌词”的 xmlns 命名空间方面做错了什么?我对 XML/XSLT 很陌生。提前致谢!

最佳答案

您需要使用 命名空间前缀 属于 的任何元素名称处的“歌词”命名空间 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>

关于xml - XSLT:匹配元素与命名空间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17738496/

相关文章:

xml - XSLT value() 和 position() 给出了不正确的索引

java - 如何避免 saxon 解析器/xalan 解析器中节点/元素之间的空格

html - XSL 中空白的奇怪 CSS 问题

php - fatal error : Class 'Vokuro\Auth\MongoDate' not found in

c++ - 对 "Str::ToInt(unsigned char*)"的 undefined reference

javascript - xpath 评估多个元素值

android 在按下选择器 xml 时更改 ImageButton 的图像

c# - Windows 10 UWP,带有汉堡菜单的后退按钮到上一个框架

XML XSLT %0A%09%09 ?使固定?

PHP 命名空间和继承