xsl:sort,为什么它不起作用?
我正在尝试使用 xslt 生成 html 页面(使用 VS 2010 作为编辑器和“编译器/转换器”)。其中大部分工作正常并生成有效的 xhtml,但是当尝试在
的帮助下生成排序列表时,顺序根本不受影响。我已经看到它有效,但是当尝试确定我的问题并创建下面的示例代码时,我的
都不起作用。
请谁能告诉我我的错误。
以下是我的示例文件。
样本.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl" ?>
<people>
<person name="Jack" age="12">
<adress>First road</adress>
</person>
<person name="Bob" age="8">
<adress>Third road</adress>
</person>
<person name="Peter" age="20">
<adress>Second road</adress>
</person>
<person name="Juli" age="65">
<adress>Last road</adress>
</person>
<person name="Abbot" age="21">
<adress>No road</adress>
</person>
</people>
样本.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output
method="xml"
omit-xml-declaration="yes" indent="yes"
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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>By @name</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="@name" data-type="text" order="ascending" />
<xsl:value-of select="@name" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
<hr />
<h2>By @age</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="@age" data-type="number" order="ascending" />
<xsl:value-of select="@age" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
<hr />
<h2>By adress</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="adress" data-type="text" order="ascending" />
<xsl:value-of select="adress" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
i'm trying to generate html pages by using xslt (using VS 2010 as the editor and 'compiler/transformer'). Most of this works well and generates valid xhtml, but when trying to generate a sorted list with the help of <xsl:sort />
the order is not affected at all. I have seen it work, but when trying to determine my problem and creating the sample code below, none of my <xsl:sort />
worked.
Please, who can show me my error.
Below are my sample files.
sample.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl" ?>
<people>
<person name="Jack" age="12">
<adress>First road</adress>
</person>
<person name="Bob" age="8">
<adress>Third road</adress>
</person>
<person name="Peter" age="20">
<adress>Second road</adress>
</person>
<person name="Juli" age="65">
<adress>Last road</adress>
</person>
<person name="Abbot" age="21">
<adress>No road</adress>
</person>
</people>
sample.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output
method="xml"
omit-xml-declaration="yes" indent="yes"
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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>By @name</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="@name" data-type="text" order="ascending" />
<xsl:value-of select="@name" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
<hr />
<h2>By @age</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="@age" data-type="number" order="ascending" />
<xsl:value-of select="@age" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
<hr />
<h2>By adress</h2>
<div>
<xsl:for-each select="people/person">
<xsl:sort select="adress" data-type="text" order="ascending" />
<xsl:value-of select="adress" />
<xsl:element name="br" />
<xsl:text />
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将样式表版本更改为 1.0 在样式表中修复此问题。换句话说,更改
为
这似乎是 .net 4 xsl 转换代码中的一个错误。请参阅此相关的 stackoverflow 问题,XSL 排序问题。
似乎发生的情况是,每当您尝试按属性排序时,它实际上是按第一个子元素排序。例如,将您的 XML 更改为此,名称将正确排序:
我在这里找到了 Microsoft 关于该问题的响应,http://connect.microsoft.com/VisualStudio/feedback/details/620628/problem-with-xsl-sort-nodes-when-using-xslcompiledtransform,这导致我进行了上面建议的修复。您可以尝试使用样式表的版本号来查看哪些有效,哪些无效。
You can fix this in your stylesheet by changing the stylesheet version to 1.0. In other words, change
To
This appears to be a bug in the .net 4 xsl transform code. See this related stackoverflow question, Problem with XSL sorting.
What appears to happen is whenever you attempt to sort by an attribute, it actually sorts by the first child element. For example, change your XML to this and the names will sort properly:
I found a response from Microsoft about the issue here, http://connect.microsoft.com/VisualStudio/feedback/details/620628/problem-with-xsl-sort-nodes-when-using-xslcompiledtransform, which led me to the fix I suggested above. You might try playing around with the version number for the stylesheet to see what does and does not work.