我有一个简单的xml文件:
<headlines>
<headline>
...
</headline>
<headline>
...
</headline>
<headline>
...
</headline>
</headlines>
我想做的就是改变标题出现的顺序。我一直在愚弄这个模板应该是什么样子,但我无法让它运作起来。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/headline">
<xsl:for-each select="headline">
__not sure what to do here? rand() isn't a thing?__
答案 0 :(得分:1)
执行此操作的一种方法是使用函数generate-id()作为排序标准。假设您有以下输入:
<?xml version="1.0" encoding="ISO-8859-1"?>
<headlines>
<headline>
Limburg
</headline>
<headline>
Fukushima
</headline>
<headline>
Große Koalition
</headline>
<headline>
Flugzeugabsturz
</headline>
</headlines>
使用此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" encoding="ISO-8859-1"/>
<xsl:template match="/headlines">
<headlines>
<xsl:for-each select="headline">
<xsl:sort select="generate-id(.)"/>
<headline id="{generate-id(.)}">
<xsl:copy-of select="./text()"/>
</headline>
</xsl:for-each>
</headlines>
</xsl:template>
</xsl:stylesheet>
结果是
<?xml version="1.0" encoding="ISO-8859-1"?>
<headlines><headline id="idm3928">
Limburg
</headline><headline id="idm5512">
Flugzeugabsturz
</headline><headline id="idm5704">
Große Koalition
</headline><headline id="idm5920">
Fukushima
</headline></headlines>
应该注意三件事: