【问题标题】:xslt not showing results. xpath or ns wrong?xslt 没有显示结果。 xpath或ns错了?
【发布时间】:2013-08-16 11:57:06
【问题描述】:

我有以下 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>

【问题讨论】:

    标签: xslt xpath namespaces


    【解决方案1】:

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

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

    现在,在您的 XSLT 中,您还声明了这个名称空间,但没有前缀。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns="http://www.elsevier.com/xml/ani/ani" 
    

    这意味着它仅适用于您正在输出的元素,因此您的 searchresult 元素会在此命名空间中输出

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

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

    在 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" 行,但这意味着您的 searchresult (和其他)元素将在没有命名空间的情况下输出,因此您需要将其输出为 &lt;ani:searchresult&gt; 如果你希望它在给定的命名空间中。

    【讨论】:

      猜你喜欢
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多