【问题标题】:Cannot select a node using XSLT无法使用 XSLT 选择节点
【发布时间】:2012-01-09 23:51:32
【问题描述】:

我在创建 XSLT 样式表方面需要帮助。即,我无法选择节点并为其应用模板。这是输入和输出 xmls 以及我的 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <searchresult>
        <query>
            <xsl:value-of select="/feed/title"/>
        </query>
        <xsl:apply-templates select="doc-template"/>
    </searchresult>
</xsl:template>
<xsl:template name="doc-template" match="entry">
    <document>
        <title>
            <xsl:value-of select="title"/>
        </title>
        <snippet>
            <xsl:value-of select="content"/>
        </snippet>
    </document>
</xsl:template>
</xsl:stylesheet>

XML 文件是:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:google="http://base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/" xmlns:georss="http://www.georss.org/georss">
    <title>salsa - Twitter Search</title>
    <entry>
        <id>tag:search.twitter.com,2005:156464188011708416</id>
        <published>2012-01-09T19:55:44Z</published>
        <link type="text/html" href="http://twitter.com/RodFF/statuses/156464188011708416" rel="alternate"/>
        <title>Al fin ponen mi tanda! @Anelito_Amed @samueltejeira @Elieser_Soriano @jose2734 #salsa #frankieruiz</title>
        <content type="html">Al fin ponen mi tanda! @&lt;a class=" " href="http://twitter.com/Anelito_Amed"&gt;Anelito_Amed&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/samueltejeira"&gt;samueltejeira&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/Elieser_Soriano"&gt;Elieser_Soriano&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/jose2734"&gt;jose2734&lt;/a&gt; #&lt;em&gt;salsa&lt;/em&gt; &lt;a href="http://search.twitter.com/search?q=%23frankieruiz" title="#frankieruiz" class=" "&gt;#frankieruiz&lt;/a&gt;</content>
        <updated>2012-01-09T19:55:44Z</updated>
        <link type="image/png" href="http://a1.twimg.com/profile_images/1672210480/IMG-20111008-00231_normal.jpg" rel="image"/>
        <twitter:geo></twitter:geo>
        <twitter:metadata>
            <twitter:result_type>recent</twitter:result_type>
        </twitter:metadata>
        <twitter:source>&lt;a href="http://blackberry.com/twitter" rel="nofollow"&gt;Twitter for BlackBerry&#xae;&lt;/a&gt;</twitter:source>
        <twitter:lang>es</twitter:lang>
        <author>
            <name>RodFF (Rodolfo Franceschi )</name>
            <uri>http://twitter.com/RodFF</uri>
        </author>
    </entry>
    <entry>
        <id>tag:search.twitter.com,2005:156464182433288194</id>
        <published>2012-01-09T19:55:42Z</published>
        <link type="text/html" href="http://twitter.com/Olqa_N/statuses/156464182433288194" rel="alternate"/>
        <title>No causo el efecto que imaginaste No me hizo el danio que tu pensaste no llore mas de lo que creiste no hiciste falta cuando te fuiste#salsa</title>
        <content type="html">No causo el efecto que imaginaste No me hizo el danio que tu pensaste no llore mas de lo que creiste no hiciste falta cuando te fuiste#&lt;em&gt;salsa&lt;/em&gt;</content>
        <updated>2012-01-09T19:55:42Z</updated>
        <link type="image/png" href="http://a3.twimg.com/profile_images/1734507487/IMG01812-20110621-1025_jpg_normal.jpg" rel="image"/>
        <twitter:geo></twitter:geo>
        <twitter:metadata>
            <twitter:result_type>recent</twitter:result_type>
        </twitter:metadata>
        <twitter:source>&lt;a href="http://ubersocial.com" rel="nofollow"&gt;&#xdc;berSocial for BlackBerry&lt;/a&gt;</twitter:source>
        <twitter:lang>es</twitter:lang>
        <author>
            <name>Olqa_N (Olqa Lopez)</name>
            <uri>http://twitter.com/Olqa_N</uri>
        </author>
    </entry>
</feed>

【问题讨论】:

  • 主要问题是您没有使用源文档中定义的任何命名空间。至少你需要知道xmlns="http://www.w3.org/2005/Atom"。一定要使用 XSLT 1.0,还是可以使用 XSLT 2.0?

标签: xml xslt


【解决方案1】:

正如@Zachary Young 所说,当目标元素具有非空命名空间 URI 时,您需要在 XSLT 路径中使用命名空间前缀。

以下文档包含一个 xmlns:atom 声明,其中包含源文档中使用的 URI。它还在 XPath 步骤中使用 atom: 前缀。我还进行了其他修改,以实现我认为您尝试使用“XSLT”类型的惯用语所做的事情。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
     xmlns:atom="http://www.w3.org/2005/Atom">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

<xsl:template match="/atom:feed">
<searchresult>
 <query>
    <xsl:value-of select="atom:title"/>
  </query>
    <xsl:apply-templates select="atom:entry"/>
</searchresult>
</xsl:template>

<xsl:template match="atom:entry">
<document>
  <title>
    <xsl:value-of select="atom:title"/>
  </title>
  <snippet>
    <xsl:value-of select="atom:content"/>
  </snippet>
</document>
</xsl:template>
</xsl:stylesheet>

它在我们的 XML 上产生以下输出(我已缩进):

<searchresult xmlns:atom="http://www.w3.org/2005/Atom">
    <query>salsa - Twitter Search</query>
    <document>
        <title>Al fin ponen mi tanda! @Anelito_Amed @samueltejeira @Elieser_Soriano @jose2734 #salsa #frankieruiz</title>
        <snippet>Al fin ponen mi tanda! @&lt;a class=" " href="http://twitter.com/Anelito_Amed"&gt;Anelito_Amed&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/samueltejeira"&gt;samueltejeira&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/Elieser_Soriano"&gt;Elieser_Soriano&lt;/a&gt; @&lt;a class=" " href="http://twitter.com/jose2734"&gt;jose2734&lt;/a&gt; #&lt;em&gt;salsa&lt;/em&gt; &lt;a href="http://search.twitter.com/search?q=%23frankieruiz" title="#frankieruiz" class=" "&gt;#frankieruiz&lt;/a&gt;</snippet>
    </document>
    <document>
        <title>No causo el efecto que imaginaste No me hizo el danio que tu pensaste no llore mas de lo que creiste no hiciste falta cuando te fuiste#salsa</title>
        <snippet>No causo el efecto que imaginaste No me hizo el danio que tu pensaste no llore mas de lo que creiste no hiciste falta cuando te fuiste#&lt;em&gt;salsa&lt;/em&gt;</snippet>
    </document>
</searchresult>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    相关资源
    最近更新 更多