繁体   English   中英

XSL-for-each不起作用

[英]XSL - for-each not working

我有以下XML代码(test.xml):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>http://www.url.com/1/</url>
    <url>http://www.url.com/2/</url>
    <url>http://www.url.com/3/</url>
</urlset>

然后,我想用以下XSL代码(test.xsl)赋予它某种风格:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
    <xsl:for-each select="/urlset/url">
        <div>
            one address here (no matter which)
        </div>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

由于某种原因,没有结果,只有blank page

我只是遵循了几年前尝试的对我有用的示例代码,但这是行不通的。

关于如何解决这个问题的任何想法?

我只是遵循了几年前尝试的对我有用的示例代码

该代码几年前就起作用了,大概是因为那时,您的输入文档没有名称空间。

现在,您的输入文档具有一个默认名称空间 ,您需要在XSLT样式表中考虑该名称空间 如果在样式表中重新声明此命名空间并为来自输入文档的所有元素名称添加前缀 ,则样式表将起作用。

我猜您可能想要以下样式表,该样式表输出所有url元素的内容:

XSLT样式表

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:stmp="http://www.sitemaps.org/schemas/sitemap/0.9">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
    <xsl:for-each select="/stmp:urlset/stmp:url">
        <div>
            <xsl:value-of select="."/>
        </div>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XHTML输出

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:stmp="http://www.sitemaps.org/schemas/sitemap/0.9">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>Test</title>
   </head>
   <body>
      <div>http://www.url.com/1/</div>
      <div>http://www.url.com/2/</div>
      <div>http://www.url.com/3/</div>
   </body>
</html>

但是您的XSLT编程风格也可以得到改善。 代替使用不必要的xsl:for-each ,使用xsl:apply-templates并为url元素编写一个单独的模板。

XSLT样式表(已改进)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:stmp="http://www.sitemaps.org/schemas/sitemap/0.9">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
    <xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="stmp:url">
    <div xmlns="http://www.w3.org/1999/xhtml">
        <xsl:value-of select="."/>
    </div>
</xsl:template>

</xsl:stylesheet>

输出将是相同的。 您可以在此处在线尝试。

这是您要完成的简单方法:

您的XML代码:

<?xml version="1.0" encoding="UTF-8"?>
  <urlset>
    <url>http://www.url.com/1/</url>  
  </urlset>

您的XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 <xsl:template match="/">
  <html> 
   <body>
      <xsl:value-of select="urlset/url"/>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM