我遇到了问题。我想要的是总结对象并为每次迭代打印它们。我是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>
希望有人得到我的想法,并可以帮助我!我用谷歌搜索了几页,并尝试了没有运气的参数。我非常渴望让它发挥作用!任何帮助都是折旧的!谢谢:)
答案 0 :(得分: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="'
'"/>
<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="'
'"/>
</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>