简体   繁体   中英

XSL - for-each not working

I have the following XML code (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>

Then, I wanna give it some style with the following XSL code (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>

For some reason there is no result, just a blank page .

I just followed a sample code I tried few years ago that worked for me, but this doesn't work.

Any idea on how to solve this?

I just followed a sample code I tried few years ago that worked for me

The code worked a few years ago presumably because back then, your input document did not have a namespace.

Now, your input document has a default namespace that you need to account for in your XSLT stylesheet. Your stylesheet works if you redeclare this namespace in the stylesheet and prefix all element names coming from the input document.

I guess you probably want the following stylesheet, which outputs the contents of all url elements:

XSLT Stylesheet

<?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 Output

<?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>

But your style of XSLT programming could also be improved. Instead of an unnecessary xsl:for-each , use xsl:apply-templates and write a separate template for url elements.

XSLT Stylesheet (improved)

<?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>

The output will be the same. Try it yourself online here .

This is a simple way of what you are trying to accomplish:

Your XML code:

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

Your XSLT code:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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