错误:前缀必须解析为命名空间

sha*_*yte 3 xml xslt

我有以下XML要通过XSLT进行转换.我的主要目的是仅列出网址列表.它表示包含"http://"的任何行.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<urlset xmlns="http://www.google.com" xmlns:image="http://www.google1.com" xmlns:video="http://www.google2.com" xmlns:xhtml="http://www.google3.com">
  <url>
    <loc id="837">http://url1</loc>
  </url>
  <url>
    <loc id="2332">http://url2</loc>
    <image:image>
      <image:loc>http://url3</image:loc>
    </image:image>
    <image:image>
      <image:loc>http://url4</image:loc>
    </image:image>    
  </url>
</urlset>
Run Code Online (Sandbox Code Playgroud)

我创建了一个XSLT如下;

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

    <xsl:template match="/">
        <html>
            <body>
                <h1>URLS</h1>
               <ul>                    
                 <xsl:for-each select="urlset/url">
                    <li>
                        <xsl:value-of select="loc"/>
                    </li>
                 </xsl:for-each>
                  <xsl:for-each select="urlset/url/image:image">
                    <li>
                        <xsl:value-of select="image:loc"/>
                    </li>
                 </xsl:for-each>
               </ul>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet> 
Run Code Online (Sandbox Code Playgroud)

第一个foreach没有返回任何东西, 第二个foreach给出了例外:

SystemId未知; 第15行; 第53栏; 前缀必须解析为命名空间:image

有人可以帮助解决这个XSLT失败的原因吗?

kjh*_*hes 6

要解决即时错误,请将命名image空间前缀的命名空间定义添加到样式表:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:image="http://www.google.com">
Run Code Online (Sandbox Code Playgroud)

消除该错误后,还需要进行许多其他调整.在XML中为同一个(可疑的)命名空间定义这么多名称空间前缀是没有意义的.

像以下输入XML这样的东西会更合理:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<urlset xmlns="http://www.example.com/urlset"
        xmlns:image="http://www.example.com/image">
  <url>
    <loc id="837">url1</loc>
  </url>
  <url>
    <loc id="2332">url2</loc>
    <image:image>
      <image:loc>url3</image:loc>
    </image:image>
    <image:image>
      <image:loc>url4</image:loc>
    </image:image>    
  </url>
</urlset>
Run Code Online (Sandbox Code Playgroud)

然后,以下XSLT,

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:u="http://www.example.com/urlset"
                xmlns:image="http://www.example.com/image"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/u:urlset">
    <html>
      <body>
        <h1>URLS</h1>
        <ul>                    
          <xsl:for-each select="u:url">
            <li>
              <xsl:value-of select="u:loc"/>
            </li>
          </xsl:for-each>
          <xsl:for-each select="u:url/image:image">
            <li>
              <xsl:value-of select="image:loc"/>
            </li>
          </xsl:for-each>
        </ul>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet> 
Run Code Online (Sandbox Code Playgroud)

将产生您寻求的URL列表:

<html xmlns:u="http://www.example.com/urlset" xmlns:image="http://www.example.com/image">
   <body>
      <h1>URLS</h1>
      <ul>
         <li>url1</li>
         <li>url2</li>
         <li>url3</li>
         <li>url4</li>
      </ul>
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

您还可以考虑使用基于模板匹配的XSLT组织,而不是基于循环的组织作为样式.对于像这样的小例子,复杂性差别不大,但对于更大的问题,基于匹配的组织更清晰,更清晰.