2014-04-14 9 views
0

我已經搜索了一個廣泛的例子,迄今爲止沒有運用總和模板來使我的XSTL工作。XSL1.0中模板結果的滾動總和

這是XML(行數在每個planfeature不同)

<PlanFeatures> 
<PlanFeature name="Line0001"> 
    <CoordGeom> 
    <Line> 
    <Start pntRef="7540">5605 8950 1020</Start> 
    <End pntRef="7541">5605 8951 1019</End> 
    </Line> 
    <Line> 
    <Start pntRef="7541">5605 8951 1019</Start> 
    <End pntRef="7542">5605 8947 1019</End> 
    </Line> 
    <Line> 
    <Start pntRef="7542">5605 8947 1019</Start> 
    <End pntRef="7543">5605 8940 1011</End> 
    </Line> 
    <Line> 
    <Start pntRef="7543">5605 8940 1011</Start> 
    <End pntRef="7544">5605 8931 1020</End> 
    </Line> 
    </CoordGeom> 
</PlanFeature> 
</PlanFeatures> 

這是我在哪裏與XSL,它使用一個遞歸調用模板來計算每個線段的距離。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:landxml="http://www.landxml.org/schema/LandXML-1.2" xmlns:hexagon="http://xml.hexagon.com/schema/HeXML-1.5" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" version="1.0" encoding="UTF-16" indent="no" omit-xml-declaration="yes"/> 

<xsl:variable name="XML" select="/"/> 
<xsl:variable name="fileExt" select="'txt'"/> 
<xsl:variable name="fileDesc" select="'line distance report'"/> 

<xsl:template match="/"> 

<xsl:for-each select="$XML"> 
    <xsl:for-each select="landxml:LandXML/landxml:PlanFeatures/landxml:PlanFeature"> 
     <xsl:value-of select="@name"/><xsl:text>::</xsl:text> 
     <xsl:for-each select="landxml:CoordGeom/landxml:Line"> 
      <xsl:value-of select="landxml:Start/@pntRef"/><xsl:text>-</xsl:text> 
      <xsl:variable name="lista" select="landxml:Start"/>   
      <xsl:variable name="x1" select="substring-before($lista,' ')"/> 
      <xsl:variable name="yt1" select="substring-after($lista,' ')"/> 
      <xsl:variable name="y1" select="substring-before($yt1,' ')"/> 
      <xsl:variable name="z1" select="substring-after($yt1,' ')"/>  
      <xsl:variable name="listb" select="landxml:End"/> 
      <xsl:value-of select="landxml:End/@pntRef"/><xsl:text>: </xsl:text> 
      <xsl:variable name="x2" select="substring-before($listb,' ')"/> 
      <xsl:variable name="yt2" select="substring-after($listb,' ')"/> 
      <xsl:variable name="y2" select="substring-before($yt2,' ')"/> 
      <xsl:variable name="z2" select="substring-after($yt2,' ')"/> 
      <xsl:variable name="seg" select= "((($x2 - $x1)*($x2 - $x1))+(($y2 - $y1)*($y2 - $y1))+(($z2 - $z1)*($z2 - $z1)))"/> 

      <xsl:call-template name="root"> 
       <xsl:with-param name="X" select="$seg"/> 
      </xsl:call-template> 
      <xsl:text>, </xsl:text> 
     </xsl:for-each> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:for-each> 
</xsl:for-each> 
</xsl:template> 
<xsl:template name="root"> 
     <xsl:param name="X"/> 
     <xsl:param name="xn" select="0"/> 
     <xsl:param name="xn_1" select="($X+1) div 2"/>  
     <xsl:choose> 
       <xsl:when test="string(number($X)) = 'NaN'"> 
         <xsl:value-of select=" ' ' "/> 
       </xsl:when> 
       <xsl:when test="($xn_1 - $xn) * ($xn_1 - $xn) &lt; 0.00000001"> 
         <xsl:value-of select='format-number($xn_1, "#.000")'/>   
       </xsl:when> 
       <xsl:otherwise> 
         <xsl:call-template name="root"> 
           <xsl:with-param name="X" select="$X"/> 
           <xsl:with-param name="xn" select="$xn_1"/> 
           <xsl:with-param name="xn_1" select="($xn_1 + ($X div $xn_1)) div 2"/> 
         </xsl:call-template> 
       </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

我需要從根呼叫模板中總結X(distance)的值,以創建一個表示每個線段總和的值。我想我需要使用一個匹配模板,但到目前爲止,它的手甚至已經接近工作。

當前正在導出LINEID :: StartPt-EndPt:dist,StartPt-EndPt:dist等。我需要在每行的末尾顯示'dist'的總和。正如下面

Line0001::7540-7541: 1.414, 7541-7542: 2.000, 7542-7543: 12.042, 7543-7544: 12.720 

但我想

Line0001::7540-7541: 1.414, 7541-7542: 2.000, 7542-7543: 12.042, 7543-7544: 12.728 -- 28.184 

任何幫助,將不勝感激......本網站上的例子已經幫了我這麼大了,但我似乎無法通過獲得這個路障。

乾杯,

克里斯

+0

您可能要首先解釋具體輸入的預期結果。就目前而言,人們只是略過並繼續前進,因爲目前還不清楚你想要什麼。 –

+0

謝謝您的建議。我希望現在更清楚些。 – Surveyor1

+0

您示例輸出中的數字似乎與您輸入中的值不匹配(並且您的示例總和看起來不匹配,但我是否正確假設它們不應該完全匹配? – JLRishe

回答

0

你可以用一些相對簡單的遞歸和參數傳遞做到這一點。試着用這四種模板替換您的第一個模板:

<xsl:template match="/"> 
    <xsl:for-each select="$XML"> 
     <xsl:apply-templates 
      select="landxml:LandXML/landxml:PlanFeatures/landxml:PlanFeature" /> 
    </xsl:for-each> 
    </xsl:template> 

    <xsl:template match ="landxml:PlanFeature"> 
    <xsl:value-of select="concat(@name, '::')" /> 
    <xsl:apply-templates select="landxml:CoordGeom/landxml:Line[1]" /> 
    <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

    <xsl:template match="landxml:Line"> 
    <xsl:param name="total" select="0" /> 

    <xsl:value-of 
     select="concat(landxml:Start/@pntRef, '-', landxml:End/@pntRef, ': ')"/> 

    <xsl:variable name="len"> 
     <xsl:call-template name="root"> 
     <xsl:with-param name="X"> 
      <xsl:call-template name="seg" /> 
     </xsl:with-param> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:value-of select="$len"/> 

    <xsl:variable name="next" select="following-sibling::landxml:Line[1]" /> 
    <xsl:variable name="newTot" select="$total + $len" /> 
    <xsl:choose> 
     <xsl:when test="$next"> 
     <xsl:text>, </xsl:text> 
     <xsl:apply-templates select="$next"> 
      <xsl:with-param name="total" select="$newTot" /> 
     </xsl:apply-templates> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="concat(' -- ', format-number($newTot, '#.000'))" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template name="seg"> 
    <xsl:variable name="lista" select="landxml:Start"/> 
    <xsl:variable name="x1" select="substring-before($lista,' ')"/> 
    <xsl:variable name="yt1" select="substring-after($lista,' ')"/> 
    <xsl:variable name="y1" select="substring-before($yt1,' ')"/> 
    <xsl:variable name="z1" select="substring-after($yt1,' ')"/> 
    <xsl:variable name="listb" select="landxml:End"/> 
    <xsl:variable name="x2" select="substring-before($listb,' ')"/> 
    <xsl:variable name="yt2" select="substring-after($listb,' ')"/> 
    <xsl:variable name="y2" select="substring-before($yt2,' ')"/> 
    <xsl:variable name="z2" select="substring-after($yt2,' ')"/> 

    <xsl:value-of select= "($x2 - $x1)*($x2 - $x1)+ 
          ($y2 - $y1)*($y2 - $y1)+ 
          ($z2 - $z1)*($z2 - $z1)"/> 
    </xsl:template> 

當你的樣品輸入運行(後調整它來匹配您的XSLT的路徑),其結果是:

Line0001::7540-7541: 1.414, 7541-7542: 4.000, 7542-7543: 10.630, 7543-7544: 12.728 -- 28.772