xslt 没有显示结果。 xpath 或 ns 错误?

标签 xslt xpath namespaces

我有以下 xml 和 xslt 来呈现它,但没有结果。我一遍又一遍地检查,发现不是路径问题,xsl 通过了编译器。所以我不确定是命名空间问题还是其他问题。很多谢谢!

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<bibdataset xsi:schemaLocation="http://www.elsevier.com/xml/ani/ani http://www.elsevier.com/xml/ani/embase_com.xsd" 
xmlns="http://www.elsevier.com/xml/ani/ani" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ce="http://www.elsevier.com/xml/ani/common" 
xmlns:ait="http://www.elsevier.com/xml/ani/ait">
    <item>
        <bibrecord>
            <item-info>
                <itemidlist><ce:doi>10.1258/0268355042555000</ce:doi>
                </itemidlist>
            </item-info>
                <head>
                    <citation-title>
                        <titletext xml:lang="en" original="y">Effect of seasonal variations on the emergence of deep venous thrombosis of the lower extremity
                        </titletext>
                    </citation-title>
                    <abstracts>
                        <abstract xml:lang="en" original="y">
                            <ce:para>Objective: We aimed to determine the role of seasonal and meteorological variations in the incidence of lower extremity
                            </ce:para> 
                        </abstract>
                    </abstracts>
               </head>
         </bibrecord>
    </item>
</bibdataset>

XSLT 文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.elsevier.com/xml/ani/ani" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:ce="http://www.elsevier.com/xml/ani/common" 
    xmlns:ait="http://www.elsevier.com/xml/ani/ait">

  <xsl:output indent="yes" omit-xml-declaration="no"
       media-type="application/xml" encoding="UTF-8" />

  <xsl:template match="/">
    <searchresult>
      <xsl:apply-templates 
        select="/bibdataset/item/bibrecord" />
    </searchresult>
  </xsl:template>

  <xsl:template match="bibrecord">
    <document>
      <title><xsl:value-of select="head/citation-title/titletext" /></title>
      <snippet>
          <xsl:value-of select="head/abstracts/abstract/ce:para" />
      </snippet>
      <url>
        <xsl:variable name="doilink" select="item-info/itemidlist/ce:doi"/>
        <xsl:value-of 
          select="concat('http://dx.doi.org/', $doilink)" />
      </url>
    </document>
  </xsl:template>
</xsl:stylesheet>

最佳答案

这确实是命名空间的问题。在您的 XML 中,您已经声明了一个默认命名空间,表示根元素及其所有后代,并在此命名空间中。

<bibdataset .xmlns="http://www.elsevier.com/xml/ani/ani"  ...

现在,在您的 XSLT 中,您还声明了这个 namespace ,但没有前缀。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.elsevier.com/xml/ani/ani" 

这意味着它仅适用于您正在输出的元素,因此您的 搜索结果 元素在此命名空间中获取输出
<searchresult xmlns="http://www.elsevier.com/xml/ani/ani"

但是,它不适用于 XSLT 中的 xpath 表达式,因此它们会在输入 XML 中查找没有 namespace 的元素。

在 XSLT 2.0 中,解决方案是简单地声明一个“xpath-default-namespace”
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.elsevier.com/xml/ani/ani"
    xpath-default-namespace="http://www.elsevier.com/xml/ani/ani" ...

在 XSLT 1.0 中,您必须使用前缀声明命名空间,并在所有 xpath 表达式中使用此前缀。

试试这个 XSLT
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.elsevier.com/xml/ani/ani" 
    xmlns:ani="http://www.elsevier.com/xml/ani/ani" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:ce="http://www.elsevier.com/xml/ani/common" 
    xmlns:ait="http://www.elsevier.com/xml/ani/ait"
    exclude-result-prefixes="ani">

  <xsl:output indent="yes" omit-xml-declaration="no"
       media-type="application/xml" encoding="UTF-8" />

  <xsl:template match="/">
    <searchresult>
      <xsl:apply-templates 
        select="/ani:bibdataset/ani:item/ani:bibrecord" />
    </searchresult>
  </xsl:template>

  <xsl:template match="ani:bibrecord">
    <document>
      <title><xsl:value-of select="ani:head/ani:citation-title/ani:titletext" /></title>
      <snippet>
          <xsl:value-of select="ani:head/ani:abstracts/ani:abstract/ce:para" />
      </snippet>
      <url>
        <xsl:variable name="doilink" select="ani:item-info/ani:itemidlist/ce:doi"/>
        <xsl:value-of 
          select="concat('http://dx.doi.org/', $doilink)" />
      </url>
    </document>
  </xsl:template>
</xsl:stylesheet>

请注意,您可以删除行 xmlns="http://www.elsevier.com/xml/ani/ani"但这意味着您的 搜索结果 (和其他)元素将在没有命名空间的情况下输出,因此您需要将其输出为 <ani:searchresult>如果你想在给定的命名空间中使用它。

关于xslt 没有显示结果。 xpath 或 ns 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272796/

相关文章:

xslt - 如何将 Diazo 主题参数插入到某些主题的类属性中?

c# - 如何使用 XmlDocument 将多个命名空间添加到根元素?

php - 调用在另一个命名空间中定义的不带前缀的 PHP 函数

xml - xsl 应用模板的问题

xml - 如果节点有某些子节点,则拆分该节点

java - Selenium web自动化,获取特定的HTML元素

python - 在Python中的Xpath中选择标签之间的文本

xml - XPath 表达式添加到已查找城市的代码中,也可能检索附近的邮政编码

c# - 为什么 Resharper 代码清理会在 ascx 文件中添加不必要的导入命名空间?

xml - 如何将参数传递给 XSLT?