Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在XSLT1.0中,如何通过xpath获得id多次引用的属性值的总和?_Xpath_Xslt 1.0 - Fatal编程技术网

在XSLT1.0中,如何通过xpath获得id多次引用的属性值的总和?

在XSLT1.0中,如何通过xpath获得id多次引用的属性值的总和?,xpath,xslt-1.0,Xpath,Xslt 1.0,我真的希望我的标题至少有点清楚 重要提示:我只能使用xslt 1.0,因为项目需要使用MSXML xslt处理器。 我想做的是: 我生成包含房间信息的文档。房间有墙,我需要每个房间的墙面积之和 我得到的输入xml文件是由另一个程序动态创建的。 更改输入xml文件的结构不是解决方案,相信我,这是需要的,而且比我在这里介绍的要复杂得多 My XML(必须对wall元素中的innerArea属性进行汇总): [...] [...] [...] 使用XSLT,我能够获得属于房间的墙的值。 但我真的不

我真的希望我的标题至少有点清楚

重要提示:我只能使用xslt 1.0,因为项目需要使用MSXML xslt处理器。

我想做的是: 我生成包含房间信息的文档。房间有墙,我需要每个房间的墙面积之和

我得到的输入xml文件是由另一个程序动态创建的。 更改输入xml文件的结构不是解决方案,相信我,这是需要的,而且比我在这里介绍的要复杂得多

My XML(必须对wall元素中的innerArea属性进行汇总):


[...]
[...]
[...]
使用XSLT,我能够获得属于房间的墙的值。 但我真的不知道如何从中得到价值的总和

我的XSLT:

<xsl:for-each select="flat/Room">
    <xsl:for-each select="WallSegments/WallSegment">
        <xsl:variable name="curWallSegId" select="@id"/>
        <xsl:for-each select="/root/components/wallSegment[@id = $curWallSegId]">
            <xsl:variable name="curWallId" select="@wall"/>
            <xsl:for-each select="/root/components/Wall[@id = $curWallId]">
                <!--I didn't expect that this was working, but at least I tried :D-->
                <xsl:value-of select="sum(@AreaInner)"/> 
              </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
        </xsl:for-each>

所需的输出应该类似于

[...]
<paragraph>
    Room 1:
      Wall area: 51.09 m²
    [...]
</paragraph>
[...]
[…]
第1房间:
墙面面积:51.09平方米
[...]
[...]

所以我希望我正确地描述了我的问题。如果没有:对不起,你可以打我的脸(x)

如果你需要的是每个房间的面积,这是一种获得它的方法:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/root/floor">
        <xsl:for-each select="flat/Room">
            <xsl:variable name="currentRoomSegmentsIds" select="WallSegments/WallSegment/@id"/>
            <xsl:variable name="currentRoomWallsIds" select="/root/components/wallSegment[@id = $currentRoomSegmentsIds]/@wall"/>
            <xsl:variable name="currentRoomWallsInnerAreas" select="/root/components/Wall[@id = $currentRoomWallsIds]/@innerArea"/>
            Id of the room = <xsl:value-of select="@id"/>.
            Area of the room = <xsl:value-of select="sum($currentRoomWallsInnerAreas)"/>
        </xsl:for-each> <!-- Enf of for each room -->
    </xsl:template>
</xsl:stylesheet>

房间的Id=。
房间面积=
这将产生以下结果:

房间的Id=49。
房间面积=51.08648

最好使用获取“相关”数据。将其放置在样式表顶部,任何模板之外:

<xsl:key name="wall" match="components/Wall" use="@id" />
<xsl:key name="wallSegment" match="components/wallSegment" use="@id" />

然后:


房间
:
  墙面积:


将返回:

<paragraph>Room 1:
  Wall area: 51.09m²</paragraph>
1号房间:
墙面面积:51.09m²

为什么所有墙段构件都具有相同的id?--还要注意的是,您的输入格式不正确-墙组件元素没有关闭。只是打字错误和copypasto的组合;-)但是谢谢你告诉我。我的原始数据具有唯一的ID,并且格式良好。此解决方案运行良好。但是最后一个节点集的每个条目都有两个条目。。可能是visual studio的调试器。使用示例中提供的XML,第一个变量包含3个值(45、42、39),第二个变量包含3个值(20、21、22),第三个变量也包含3个值(20.7654、12.45678、17.8643)。在另一个示例中,前两个变量可能有重复的值,但第三个变量将只具有不同墙的内部区域,只要/root/components没有两个具有相同id的墙。我将在明天进行尝试。使用钥匙完成这类任务有什么好处?好的,这对我来说很有效。谢谢你们两位的大力帮助!
<xsl:for-each select="flat/Room">
    <paragraph>
        <xsl:text>Room </xsl:text>
        <xsl:value-of select="position()"/>
        <xsl:text>:&#10;  Wall area: </xsl:text>
        <xsl:value-of select="format-number(sum(key('wall', key('wallSegment', WallSegments/WallSegment/@id)/@wall)/@innerArea), '0.00m²')"/>
        <xsl:text> &#10;</xsl:text>
    </paragraph>
</xsl:for-each>
<paragraph>Room 1:
  Wall area: 51.09m²</paragraph>