从 xml 中选择指定顺序
Selecting from xml specifying an order
在 python 上使用 xslt 1.0,我正在尝试 select 一些项目,同时指定顺序:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item name='1'>
first
</item>
<item name='2'>
second
</item>
<item name='3'>
third
</item>
</items>
如果我使用 for-each 和一个大的 OR'd together 列表,我可以获得我想要的项目,但只能按照上面 xml 源文档的原始顺序。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="items/item[@name='2']|items/item[@name='1']">
<p>hi</p>
<xsl:value-of select="." />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这会产生:
hi
first
hi
second
但我想让它输出:
hi
second
hi
first
我认为使用 xsl:apply-templates 可能是可行的方法,但即使是这个简单的示例我也无法让它工作。 xslt 1.0 中按特定顺序 select 元素的最佳方法是什么?
您可以使用 <xsl:sort>
指定排序,尤其是当存在 特定逻辑来定义排序 时,例如按 name
属性值排序按降序排列:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="items/item[@name='2' or @name='1']">
<xsl:sort select="@name" data-type="number" order="descending"/>
<p>hi</p>
<xsl:value-of select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
"I think using xsl:apply-templates might be the way to go, but I can't get it to work with even this simple example"
也可以,例如:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="items/item[@name='2']"/>
<xsl:apply-templates select="items/item[@name='1']"/>
</body>
</html>
</xsl:template>
<xsl:template match="items/item">
<p>hi</p>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出:
<html>
<body>
<p>hi</p>
second
<p>hi</p>
first
</body>
</html>
在 python 上使用 xslt 1.0,我正在尝试 select 一些项目,同时指定顺序:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item name='1'>
first
</item>
<item name='2'>
second
</item>
<item name='3'>
third
</item>
</items>
如果我使用 for-each 和一个大的 OR'd together 列表,我可以获得我想要的项目,但只能按照上面 xml 源文档的原始顺序。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="items/item[@name='2']|items/item[@name='1']">
<p>hi</p>
<xsl:value-of select="." />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这会产生:
hi
first
hi
second
但我想让它输出:
hi
second
hi
first
我认为使用 xsl:apply-templates 可能是可行的方法,但即使是这个简单的示例我也无法让它工作。 xslt 1.0 中按特定顺序 select 元素的最佳方法是什么?
您可以使用 <xsl:sort>
指定排序,尤其是当存在 特定逻辑来定义排序 时,例如按 name
属性值排序按降序排列:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="items/item[@name='2' or @name='1']">
<xsl:sort select="@name" data-type="number" order="descending"/>
<p>hi</p>
<xsl:value-of select="."/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
"I think using xsl:apply-templates might be the way to go, but I can't get it to work with even this simple example"
也可以,例如:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="items/item[@name='2']"/>
<xsl:apply-templates select="items/item[@name='1']"/>
</body>
</html>
</xsl:template>
<xsl:template match="items/item">
<p>hi</p>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出:
<html>
<body>
<p>hi</p>
second
<p>hi</p>
first
</body>
</html>