0

我有问题。我想要的是对对象求和并为每次迭代打印它们。我是 XSLT 的新手,所以请善待。我所拥有的是这段代码:

<?xml version = "1.0" encoding = "Windows-1252"?>
<GPDUMP>
  <ROUTE>
    <ALL_ROUTEANDORDER>
      <NUMBER>3</NUMBER>
      <NO0>
        <ROUTEANDORDER>
          <FKCUSTOMERID>2</FKCUSTOMERID>
          <SEQUENCENO>1</SEQUENCENO>
          <STOPNO>1</STOPNO>
          <SPLITKMDRIVING>13076</SPLITKMDRIVING>
        </ROUTEANDORDER>
      </NO0>
      <NO1>
        <ROUTEANDORDER>
          <FKCUSTOMERID>4</FKCUSTOMERID>
          <SEQUENCENO>2</SEQUENCENO>
          <STOPNO>2</STOPNO>
          <SPLITKMDRIVING>6204</SPLITKMDRIVING>
        </ROUTEANDORDER>
      </NO1>
      <NO2>
        <ROUTEANDORDER>
          <FKCUSTOMERID>3</FKCUSTOMERID>
          <SEQUENCENO>3</SEQUENCENO>
          <STOPNO>3</STOPNO>
          <SPLITKMDRIVING>4775</SPLITKMDRIVING>
        </ROUTEANDORDER>
      </NO2>
    </ALL_ROUTEANDORDER>
  </ROUTE>
</GPDUMP>

我想要的输出是这样的:

<CUSTOMERID>2</CUSTOMERID>
<DIST_FROM_PREVIOUS>13076</DIST_FROM_PREVIOUS>
<DIST_FROM_START>13076</DIST_FROM_START>

<CUSTOMERID>4</CUSTOMERID>
<DIST_FROM_PREVIOUS>6204</DIST_FROM_PREVIOUS>
<DIST_FROM_START>13076 + 6204</DIST_FROM_START>

<CUSTOMERID>3</CUSTOMERID>
<DIST_FROM_PREVIOUS>4775</DIST_FROM_PREVIOUS>
<DIST_FROM_START>13076 + 6204 + 4775</DIST_FROM_START>

如您所见,DIST_FROM_START应该按照以下过程求和。

我所拥有的是:

<xsl:for-each select="ALL_ROUTEANDORDER[1]/*[$index]" >
  <xsl:variable name="kmFromStart" select="sum(ALL_ROUTEANDORDER/*[starts-with(name(),'NO')]/ROUTEANDORDER/SPLITKMDRIVING)"/>
  <xsl:variable name="kmFromPrevious" select="ALL_ROUTEANDORDER/*[starts-with(name(),'NO')]/ROUTEANDORDER/SPLITKMDRIVING"/>
  <xsl:text>From start</xsl:text><xsl:value-of select="$kmFromStart" />
  <xsl:text>From previous </xsl:text><xsl:value-of select="$kmFromPrevious" />
</xsl:for-each>

希望有人能理解我的想法,并可以帮助我!我用谷歌搜索了几页,也尝试了没有运气的参数。我真的很想让这个工作!任何帮助都将不胜感激!谢谢 :)

4

1 回答 1

2

在没有任何关于您的确切要求的指导的情况下,我写了这个。我希望它有所帮助。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/GPDUMP/ROUTE/ALL_ROUTEANDORDER">
    <root>
      <xsl:value-of select="'&#x0A;'"/>
      <xsl:apply-templates select="*[starts-with(local-name(), 'NO')]"/>
    </root>
  </xsl:template>

  <xsl:template match="*[starts-with(local-name(), 'NO')]">

    <xsl:variable name="routes" select="(self::*|preceding-sibling::*[starts-with(local-name(), 'NO')])/ROUTEANDORDER/SPLITKMDRIVING"/>

    <CUSTOMERID>
      <xsl:value-of select="ROUTEANDORDER/FKCUSTOMERID"/>
    </CUSTOMERID>
    <DIST_FROM_PREVIOUS>
      <xsl:value-of select="ROUTEANDORDER/SPLITKMDRIVING"/>
    </DIST_FROM_PREVIOUS>
    <DIST_FROM_START>
      <xsl:value-of select="sum($routes)"/>
    </DIST_FROM_START>

    <xsl:value-of select="'&#x0A;'"/>

  </xsl:template>

</xsl:stylesheet>

输出

<?xml version="1.0" encoding="utf-8"?>
<root>

   <CUSTOMERID>2</CUSTOMERID>
   <DIST_FROM_PREVIOUS>13076</DIST_FROM_PREVIOUS>
   <DIST_FROM_START>13076</DIST_FROM_START>

   <CUSTOMERID>4</CUSTOMERID>
   <DIST_FROM_PREVIOUS>6204</DIST_FROM_PREVIOUS>
   <DIST_FROM_START>19280</DIST_FROM_START>

   <CUSTOMERID>3</CUSTOMERID>
   <DIST_FROM_PREVIOUS>4775</DIST_FROM_PREVIOUS>
   <DIST_FROM_START>24055</DIST_FROM_START>

</root>
于 2013-05-10T20:19:42.237 回答