首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xslt没有显示结果。xpath还是ns错误?

xslt没有显示结果。xpath还是ns错误?
EN

Stack Overflow用户
提问于 2013-08-16 11:57:06
回答 1查看 451关注 0票数 0

我有下面的xml和xslt来呈现它,但是没有得到任何结果。我一次又一次地检查,发现没有路径问题,而xsl通过了编译器。因此,我不确定这是名称空间问题还是其他问题。很多东西!

XML文件

代码语言:javascript
复制
<?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文件

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-16 13:51:19

这确实是命名空间的一个问题。在XML中,您已经声明了一个默认名称空间,它意味着根元素及其所有后代,以及这个命名空间。

代码语言:javascript
复制
<bibdataset .xmlns="http://www.elsevier.com/xml/ani/ani"  ...

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

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.elsevier.com/xml/ani/ani" 

这意味着它只适用于您要输出的元素,因此您的搜索结果元素将在此命名空间中获得输出。

代码语言:javascript
复制
<searchresult xmlns="http://www.elsevier.com/xml/ani/ani"

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

在XSLT2.0中,解决方案是简单地声明一个“xpath-默认名称空间”

代码语言:javascript
复制
<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" ...

在XSLT1.0中,您必须用前缀声明名称空间,并在所有xpath表达式中使用该前缀。

试试这个XSLT

代码语言:javascript
复制
<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>,则需要将其输出到给定的命名空间中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18272796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档