我有一个XML,其中包含一系列图像及其宽度:
<p>
<image width="10cm"/>
<image width="3cm"/>
</p>
我需要计算这些图像的总宽度。 当我只有1张图片时,这很简单:
<template match="p">
<xsl:variable name="imgwidth">
<xsl:value-of select="number(substring-before(image/@width,'cm'))"/>
天真的,我尝试扩展它以容纳更多图像:
<xsl:value-of select="sum(number(substring-before(image/@width,'cm')))"/>
在样本上运行此命令时,出现错误消息:
不允许将一个以上项目的序列作为fn:substring-before()(“ 10cm”,“ 3cm”)的第一个参数
我已经进行了一些搜索,但无法弄清楚如何在<p>
内的每个图像节点上运行substring-before。
答案 0 :(得分:1)
在XSLT 2.0中,您可以编写此代码...
<xsl:value-of select="sum(image/number(substring-before(@width,'cm')))"/>
或者,也许这...
<xsl:value-of select="sum(for $i in image return number(substring-before($i/@width, 'cm')))"/>