我有一张这样的xml表:
<houses>
<house number="1">
<mainroom>
<roomprice>5</roomprice>
<roomtax>2</roomtax>
</mainroom>
<roompricefull>
<price value="8"/>
</roompricefull>
</house>
<house number="2">
<mainroom>
<roomprice>3</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="7"/>
</roompricefull>
</house>
<house number="3">
<mainroom>
<roomprice>9</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="4"/>
</roompricefull>
</house>
<house number="4">
<mainroom>
<roomprice>12</roomprice>
<roomtax>3</roomtax>
</mainroom>
<roompricefull>
<price value="6"/>
</roompricefull>
</house>
</houses>
所以我不得不用“roomprice”值和“roomtax”的总和来改变每个“house”中“price”元素中属性“value”的值
我写了一个这样的xsl转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="PriceChange" match="price[parent::roompricefull]">
<xsl:copy>
<xsl:variable name="sn" select="../../@number"/>
<xsl:variable name="TaxValue" select="number(//house[@number=string($sn)]/mainroom/roomtax)"/>
<xsl:variable name="BaseValue" select="number(//house[@number=string($sn)]/mainroom/roomprice)"/>
<xsl:attribute name="value">
<xsl:value-of select="string($TaxValue+$BaseValue)"/>
</xsl:attribute>
<!--xsl:for-each select="/houses/house">
<xsl:sort select="houses/house[$sn]/roompricefull/@value"/>
</xsl:for-each-->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但是当我开始按照我的新价值对“房子”元素进行排序时,我发现了问题。 我实际上不明白为什么它不起作用,所以我在上面的代码中评论了我的最后几十个例子。
我明白了:
<houses>
<house number="1">
<mainroom>
<roomprice>5</roomprice>
<roomtax>2</roomtax>
</mainroom>
<roompricefull>
<price value="7"/>
</roompricefull>
</house>
<house number="2">
<mainroom>
<roomprice>3</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="4"/>
</roompricefull>
</house>
<house number="3">
<mainroom>
<roomprice>9</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="10"/>
</roompricefull>
</house>
<house number="4">
<mainroom>
<roomprice>12</roomprice>
<roomtax>3</roomtax>
</mainroom>
<roompricefull>
<price value="15"/>
</roompricefull>
</house>
</houses>
但预期的结果是:
<houses>
<house number="4">
<mainroom>
<roomprice>12</roomprice>
<roomtax>3</roomtax>
</mainroom>
<roompricefull>
<price value="15"/>
</roompricefull>
</house>
<house number="3">
<mainroom>
<roomprice>9</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="10"/>
</roompricefull>
</house>
<house number="1">
<mainroom>
<roomprice>5</roomprice>
<roomtax>2</roomtax>
</mainroom>
<roompricefull>
<price value="7"/>
</roompricefull>
</house>
<house number="2">
<mainroom>
<roomprice>3</roomprice>
<roomtax>1</roomtax>
</mainroom>
<roompricefull>
<price value="4"/>
</roompricefull>
</house>
</houses>
如果你可以帮我分类并解释为什么我的例子不起作用,那就太好了。似乎我不理解&lt; sort /&gt;的含义,但我找到的所有内容都告诉我关于它的用法而没有任何解释。 提前谢谢。
答案 0 :(得分:4)
如果你可以帮我分类
那就太好了
这是正确的XSLT 1.0方法。
请注意正确使用需要的xsl:sort
:
xsl:apply-templates
排序顺序也指定为默认ascending
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="houses">
<xsl:copy>
<xsl:apply-templates select="house">
<xsl:sort select="mainroom/roomprice + mainroom/roomtax"
data-type="number"
order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="price/@value">
<xsl:value-of select="
../../../mainroom/roomprice
+
../../../mainroom/roomtax"/>
</xsl:template>
解释我的示例无效的原因
您的转换不起作用主要是因为您尝试在错误的上下文中对元素进行排序(在与树中的深子项匹配的模板内)。此外:
xsl:sort
属性答案 1 :(得分:0)
按新价格排序:
<xsl:template match="/">
<xsl:for-each select="house">
<xsl:sort select="mainroom/roomprice + mainroom/roomtax"/>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:template>
和以前一样:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
调整输出中的价格:
<xsl:template match="roompricefull">
<roompricefull>
<price value="{../mainroom/roomprice + ../mainroom/roomtax}"/>
</roompricefull>
</xsl:template>