[英]XSL - for-each not working
I have the following XML
code (test.xml): 我有以下
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>
Then, I wanna give it some style with the following XSL
code (test.xsl): 然后,我想用以下
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>
For some reason there is no result, just a blank page
. 由于某种原因,没有结果,只有
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. 现在,您的输入文档具有一个默认名称空间 ,您需要在XSLT样式表中考虑该名称空间 。 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: 我猜您可能想要以下样式表,该样式表输出所有
url
元素的内容:
XSLT Stylesheet 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 Output 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>
But your style of XSLT programming could also be improved. 但是您的XSLT编程风格也可以得到改善。 Instead of an unnecessary
xsl:for-each
, use xsl:apply-templates
and write a separate template for url
elements. 代替使用不必要的
xsl:for-each
,使用xsl:apply-templates
并为url
元素编写一个单独的模板。
XSLT Stylesheet (improved) 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>
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
代码:
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
<url>http://www.url.com/1/</url>
</urlset>
Your XSLT
code: 您的
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.