Loops 使用XSLT计算同级元素的值之和
我正在使用XSLT进行测量值的自动转换。将单个测量值从一个系统(例如英制)转换为另一个系统(例如公制)效果良好。但是英制单位可能采用“5英尺10英寸”的形式,我想把它转换成一个公制单位 在我的XML模型中,我通过允许单个值或多个子节点来适应这种组合测量。因此,当我发现我有子节点时,我需要将这些子元素中的每一个都转换为度量单位,然后将这些值相加,得到一个度量结果 我正在努力寻找处理多个子节点并将结果值相加的最佳方法。在迭代语言中,我只需要从第一个处理到下一个,并更新一个全局变量,但在XSLT中,我不知道是否有全局变量这样的东西可以从对同一模板的后续调用中更新 这里是一个(简化的)变换-这个变换只处理[ft_i]和[in_i]到mLoops 使用XSLT计算同级元素的值之和,loops,xslt,calculation,Loops,Xslt,Calculation,我正在使用XSLT进行测量值的自动转换。将单个测量值从一个系统(例如英制)转换为另一个系统(例如公制)效果良好。但是英制单位可能采用“5英尺10英寸”的形式,我想把它转换成一个公制单位 在我的XML模型中,我通过允许单个值或多个子节点来适应这种组合测量。因此,当我发现我有子节点时,我需要将这些子元素中的每一个都转换为度量单位,然后将这些值相加,得到一个度量结果 我正在努力寻找处理多个子节点并将结果值相加的最佳方法。在迭代语言中,我只需要从第一个处理到下一个,并更新一个全局变量,但在XSLT中,我
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*,node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="measurement">
<xsl:copy>
<xsl:choose>
<xsl:when test="measurement">
<xsl:apply-templates select="*"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="normalise">
<xsl:with-param name="val" as="xs:double" select="number(text())"/>
<xsl:with-param name="unitin" select="@ucum"/>
<xsl:with-param name="count" as="xs:integer" select="1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:template name="normalise">
<xsl:param name="val" as="xs:double"/>
<xsl:param name="unitin"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$unitin eq '[ft_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.3048"/>
</xsl:when>
<xsl:when test="$unitin eq '[in_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.0254"/>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
一个简单的测试文件:
<topic>
<p>This piece is
<measurement>
<measurement unit="ft"
ucum=" [ft_i]">10</measurement>
<measurement unit="in"
ucum="[in_i]">2</measurement>
</measurement>
long
</p>
</topic>
这件是
10
2.
长的
变换给出了以下结果:
<topic>
<p>This piece is
<measurement>
<measurement ucum="m"
unit="m">3.048</measurement>
<measurement ucum="m"
unit="m">0.0508</measurement>
</measurement>
long
</p>
</topic>
这件是
3.048
0.0508
长的
显然,我希望看到:
<topic>
<p>This piece is
<measurement ucum="m"
unit="m">3.0988</measurement>
long
</p>
</topic>
这件是
3.0988
长的
我可以在子度量节点上使用xsl:for-each,但如何将单独的值添加到全局值,然后从主模板输出?假设某些值和属性是常量,最简单的方法是使用复杂的XPath-2.0表达式。然后,您可以将
度量
模板缩减为:
<xsl:template match="measurement">
<measurement ucum="m" unit="m">
<xsl:copy-of select="sum(for $x in measurement return if (normalize-space($x/@ucum)= '[ft_i]') then xs:double(normalize-space($x))*0.3048 else xs:double(normalize-space($x))*0.0254)" />
</measurement>
</xsl:template>
它假设属性保持不变,并且只有两个单元。但是你可以很容易地扩展它
根据您的方法构建模板,也可以使用以下解决方案:
<xsl:template match="measurement">
<xsl:copy>
<xsl:variable name="summary">
<xsl:for-each select="measurement">
<val>
<xsl:call-template name="normalise">
<xsl:with-param name="val" as="xs:double" select="number(.)"/>
<xsl:with-param name="unitin" select="@ucum"/>
<xsl:with-param name="count" as="xs:integer" select="1"/>
</xsl:call-template>
</val>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="$summary/val[1]/@*" />
<xsl:copy-of select="sum($summary/val)" />
</xsl:copy>
</xsl:template>
<xsl:template name="normalise">
<xsl:param name="val" as="xs:double"/>
<xsl:param name="unitin"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:choose>
<xsl:when test="normalize-space($unitin) = '[ft_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.3048"/>
</xsl:when>
<xsl:when test="normalize-space($unitin) = '[in_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.0254"/>
</xsl:when>
</xsl:choose>
</xsl:template>
它更灵活,使用两阶段方法处理变量。
结果是一样的。我想如果你把两者结合起来,你会找到一个很好的方法来满足你的需要。假设一些值和属性是常量,最简单的方法就是使用一个复杂的XPath-2.0表达式。然后,您可以将
度量
模板缩减为:
<xsl:template match="measurement">
<measurement ucum="m" unit="m">
<xsl:copy-of select="sum(for $x in measurement return if (normalize-space($x/@ucum)= '[ft_i]') then xs:double(normalize-space($x))*0.3048 else xs:double(normalize-space($x))*0.0254)" />
</measurement>
</xsl:template>
它假设属性保持不变,并且只有两个单元。但是你可以很容易地扩展它
根据您的方法构建模板,也可以使用以下解决方案:
<xsl:template match="measurement">
<xsl:copy>
<xsl:variable name="summary">
<xsl:for-each select="measurement">
<val>
<xsl:call-template name="normalise">
<xsl:with-param name="val" as="xs:double" select="number(.)"/>
<xsl:with-param name="unitin" select="@ucum"/>
<xsl:with-param name="count" as="xs:integer" select="1"/>
</xsl:call-template>
</val>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="$summary/val[1]/@*" />
<xsl:copy-of select="sum($summary/val)" />
</xsl:copy>
</xsl:template>
<xsl:template name="normalise">
<xsl:param name="val" as="xs:double"/>
<xsl:param name="unitin"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:choose>
<xsl:when test="normalize-space($unitin) = '[ft_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.3048"/>
</xsl:when>
<xsl:when test="normalize-space($unitin) = '[in_i]'">
<xsl:attribute name="ucum">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:attribute name="unit">
<xsl:value-of select="'m'"/>
</xsl:attribute>
<xsl:value-of select="$val * 0.0254"/>
</xsl:when>
</xsl:choose>
</xsl:template>
它更灵活,使用两阶段方法处理变量。
结果是一样的。我想如果您将两者结合起来,您会找到一种很好的方法来满足您的需求。无需进行多过程处理——XPath 2.0对于此类问题已经足够强大:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="measurement[*]">
<measurement ucum="m" unit="m">
<xsl:value-of select=
"sum(measurement/(. * (if(@ucum eq '[ft_i]') then 0.3048 else 0.0254)))"/>
</measurement>
</xsl:template>
</xsl:stylesheet>
<topic>
<p>This piece is
<measurement>
<measurement unit="ft"
ucum="[ft_i]">10</measurement>
<measurement unit="in"
ucum="[in_i]">2</measurement>
</measurement>
long
</p>
</topic>
<topic>
<p>This piece is
<measurement ucum="m" unit="m">3.0988</measurement>
long
</p>
</topic>
当此转换应用于相同的XML文档(如上)时,将再次生成相同的正确的所需结果:
<topic>
<p>This piece is
<measurement ucum="m" unit="m">3.0988</measurement>
long
</p>
</topic>
这件是
3.0988
长的
不需要多通道处理——XPath 2.0对于此类问题已经足够强大:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="measurement[*]">
<measurement ucum="m" unit="m">
<xsl:value-of select=
"sum(measurement/(. * (if(@ucum eq '[ft_i]') then 0.3048 else 0.0254)))"/>
</measurement>
</xsl:template>
</xsl:stylesheet>
<topic>
<p>This piece is
<measurement>
<measurement unit="ft"
ucum="[ft_i]">10</measurement>
<measurement unit="in"
ucum="[in_i]">2</measurement>
</measurement>
long
</p>
</topic>
<topic>
<p>This piece is
<measurement ucum="m" unit="m">3.0988</measurement>
long
</p>
</topic>
当此转换应用于相同的XML文档(如上)时,将再次生成相同的正确的所需结果:
<topic>
<p>This piece is
<measurement ucum="m" unit="m">3.0988</measurement>
long
</p>
</topic>
这件是
3.0988
长的
假设您可以有两个以上的单位,将它们存储在变量中并使用以下公式检索相应的转换系数将非常方便:
XSLT2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="unit" match="unit" use="@name" />
<xsl:variable name="units">
<unit name="ft" factor=".3048"/>
<unit name="in" factor=".0254"/>
<!-- add more units here ... -->
</xsl:variable>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="measurement[measurement]">
<measurement ucum="m" unit="m">
<xsl:value-of select="sum(measurement/(. * key('unit', @unit, $units)/@factor))"/>
</measurement>
</xsl:template>
</xsl:stylesheet>
演示:假设您可以有两个以上的单位,将它们存储在变量中并使用以下公式检索相应的转换系数将非常方便: XSLT2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="unit" match="unit" use="@name" />
<xsl:variable name="units">
<unit name="ft" factor=".3048"/>
<unit name="in" factor=".0254"/>
<!-- add more units here ... -->
</xsl:variable>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="measurement[measurement]">
<measurement ucum="m" unit="m">
<xsl:value-of select="sum(measurement/(. * key('unit', @unit, $units)/@factor))"/>
</measurement>
</xsl:template>
</xsl:stylesheet>
演示:谢谢。第二种方法似乎最适合我的需要,因为我确实有一个更精细的归一化模板,而且我还必须捕获输入中可能出现的错误,其中不兼容的度量单位组合在一起。我还不知道sum()函数——当然,这就是诀窍。@4everJang In X