2012-08-15 97 views
0

我試圖將1添加到節點的上一次出現中。其背後的邏輯是,如果之前的節點在指定的三個字段上不匹配;然後將位置設置爲1。如果前一個節點與前一個節點匹配,則從前一個位置取值並添加1添加節點值和編號

我已經創建了XSLT來完成此操作,但是我不知道如何將1添加到前一個節點。

當我使用的代碼片斷:

<xsl:value-of select="preceding-sibling::orderLine/position + '1'"/> 

i的輸出中得到的響應爲:

<position>NaN</position> 

我使用XSLT是:

<?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 indent="yes"/> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template 
     match="/order/orderLines/orderLine[sku = preceding-sibling::orderLine/sku and lineId = preceding-sibling::orderLine/lineId]"/> 

    <xsl:template match="/order/orderLines"> 
     <xsl:for-each select="/order/orderLines/orderLine"> 
      <xsl:copy> 
       <xsl:apply-templates select="@* | node()"/> 


       <xsl:choose> 

        <xsl:when 
         test="lineId = preceding-sibling::orderLine/lineId and sku = preceding-sibling::orderLine/sku and lineTrackingUrl = preceding-sibling::orderLine/lineTrackingUrl"> 
         <position> 
          <!-- THE ISSUE IS HERE --> 
          <xsl:value-of select="preceding-sibling::orderLine/position + '1'"/> 
          <!-- THE ISSUE IS HERE --> 
         </position> 
        </xsl:when> 
        <xsl:otherwise> 
         <position>1</position> 
        </xsl:otherwise> 
       </xsl:choose> 

      </xsl:copy> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

實施例輸入:

<?xml version="1.0" encoding="utf-8"?> 
<order> 
    <orderLines> 
     <orderLine> 
      <lineId>4</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>4</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>4</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>4</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>3</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>3</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>3</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
     <orderLine> 
      <lineId>3</lineId> 
      <sku>1111111</sku> 
      <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     </orderLine> 
    </orderLines> 
</order> 

如果你使用它上面的輸入例子完全說明了爲什麼這不工作:

預期的結果:

<?xml version="1.0" encoding="utf-8"?> 
<order> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>0</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>1</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>2</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>3</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>0</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>1</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>2</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>3</position> 
    </orderLine> 
</order> 

實際結果:

<?xml version="1.0" encoding="utf-8"?> 
<order> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>0</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>1</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>2</position> 
    </orderLine> 
    <orderLine> 
     <lineId>4</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>3</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>0</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>5</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>6</position> 
    </orderLine> 
    <orderLine> 
     <lineId>3</lineId> 
     <sku>1111111</sku> 
     <lineTrackingUrl>![CDATA[1111111111]]</lineTrackingUrl> 
     <position>7</position> 
    </orderLine> 
</order> 

的原因,這是不工作是因爲它可以匹配前面的節點:

這是代碼:

<xsl:when 
    test="lineId = preceding-sibling::orderLine/lineId and sku = preceding-sibling::orderLine/sku and lineTrackingUrl = preceding-sibling::orderLine/lineTrackingUrl"> 
    <position> 
     <xsl:value-of select="count(preceding-sibling::orderLine/lineId)"/> 
    </position> 
</xsl:when> 

您將看到test的時候是lineId = preceding-sibling::orderLine/lineId and sku = preceding-sibling::orderLine/sku and lineTrackingUrl = preceding-sibling::orderLine/lineTrackingUrl但是一個實際計數僅僅是preceding-sibling::orderLine/lineId

所以它只是匹配所有lineIds不只是那些具有與之前相同的lineId和相同的Sku和相同的TrackingUrl,以及之前的那個......等等。我如何限制這個?

修訂版

<?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 indent="yes"/> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template 
     match="/order/orderLines/orderLine[sku = preceding-sibling::orderLine/sku and lineId = preceding-sibling::orderLine/lineId]"/> 

    <xsl:template match="/order/orderLines"> 
     <orderLines> 
     <xsl:for-each select="/order/orderLines/orderLine"> 
      <xsl:copy> 
       <xsl:apply-templates select="@* | node()"/>    

       <xsl:choose> 

        <xsl:when 
         test="lineId = preceding-sibling::orderLine[4]/lineId and sku = preceding-sibling::orderLine[4]/sku"> 
         <position> 
          <xsl:value-of select="'5'"/> 
         </position> 
        </xsl:when> 
        <xsl:when 
         test="lineId = preceding-sibling::orderLine[3]/lineId and sku = preceding-sibling::orderLine[3]/sku"> 
         <position> 
          <xsl:value-of select="'4'"/> 
         </position> 
        </xsl:when> 
        <xsl:when 
         test="lineId = preceding-sibling::orderLine[2]/lineId and sku = preceding-sibling::orderLine[2]/sku"> 
         <position> 
          <xsl:value-of select="'3'"/> 
         </position> 
        </xsl:when> 
        <xsl:when 
         test="lineId = preceding-sibling::orderLine[1]/lineId and sku = preceding-sibling::orderLine[1]/sku"> 
         <position> 
          <xsl:value-of select="'2'"/> 
         </position> 
        </xsl:when> 
        <xsl:otherwise> 
         <position><xsl:value-of select="'1'"/></position> 

        </xsl:otherwise> 
       </xsl:choose> 

      </xsl:copy> 
     </xsl:for-each> 
     </orderLines> 
    </xsl:template> 
</xsl:stylesheet> 

是否有做多的一個更合適的方法時聲明,這只是針對4項工程,有沒有做它的無限物品的方式。目前這沒什麼問題,但是如果需要擴展的話,我會怎麼做呢?

回答

0

我使用count()函數解決了這個問題,所以這行看起來是這樣的:

<xsl:value-of select="count(preceding-sibling::orderLine/lineId)"/>

這將自動填充正確的號碼爲<position/>

因此,這意味着整個XSLT看起來像這樣:

<?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 indent="yes"/> 

    <xsl:variable name="start" select="'0'"/> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template 
     match="/order/orderLines/orderLine[sku = preceding-sibling::orderLine/sku and lineId = preceding-sibling::orderLine/lineId]"/> 

    <xsl:template match="/order/orderLines"> 
     <xsl:for-each select="/order/orderLines/orderLine"> 
      <xsl:copy> 
       <xsl:apply-templates select="@* | node()"/> 


       <xsl:choose> 

        <xsl:when 
         test="lineId = preceding-sibling::orderLine/lineId and sku = preceding-sibling::orderLine/sku and lineTrackingUrl = preceding-sibling::orderLine/lineTrackingUrl"> 
         <position> 
          <xsl:value-of select="count(preceding-sibling::orderLine/lineId)"/> 
         </position> 
        </xsl:when> 
        <xsl:otherwise> 
         <position>1</position> 
        </xsl:otherwise> 
       </xsl:choose> 

      </xsl:copy> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
+0

或者,您可以使用''這個。有時候會很方便,如果你想避免手動進行計算,並提出一些微不足道的錯誤。 – 2012-08-15 12:07:49

+0

這不起作用。我已經對它做了一些測試,並且失敗了。我將在上面解釋爲什麼它失敗。 – MMKD 2012-08-15 14:32:42

+0

這很有趣 - 你可能沒有正確指定主文章底部添加的'xsl:number' – 2012-08-15 14:34:20