2011-04-27 137 views
0

我想知道我們是否有XSL 2.0中的某些東西,相當於Java中的List。我想遞歸地調用一個模板10次,並傳入一個名爲'mylist'的輸入變量。在模板中,我想要做的操作,如添加項目列表,從列表中刪除項目,迭代列表中的項目等。我可以看到像'序列'的東西,但我不知道它是否可以用來添加,刪除,迭代等。請分享你的想法來實現這一點。XSL - 列表等效實現

我嘗試使用序列的幫助下面的響應,我面臨一些語法問題,如聲明一個空序列。我想要使​​用insert-before或concat sequnce函數來打印序列1 2 3 4 5 6 7 8 9 10。請幫我修復語法。

<xsl:stylesheet version="2.0" 
    xmlns:locator="http://ntr.lxnx.org" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:variable name="output"> 
    <xsl:call-template name="calculate-data"> 
       <xsl:with-param 
        name="sequence" 
        select=""/> 
       <xsl:with-param 
        name="count" 
        select="1"/> 
      </xsl:call-template> 
    </xsl:variable> 
    <xsl:value-of select="output"></xsl:value-of> 
    </xsl:template> 
    <xsl:variable name="main-root" as="document-node()" select="/"/> 

    <xsl:template name="calculate-data"> 
<xsl:param name="sequence"/> 
    <xsl:param name="count" select="0"/> 
<xsl:if test="$count != 10"> 
       fn:insert-before($count as item()*,0 as xs:integer,$sequence as item()*) 
       <xsl:call-template name="calculate-data"> 
        <xsl:with-param 
         name="sequence" 
         select="$sequence"/> 
        <xsl:with-param 
         name="count" 
         select="$count + 1"/> 
       </xsl:call-template>    


</xsl:if> 
     </xsl:template> 
     </xsl:stylesheet> 
+0

您可以迭代序列。您將能夠將模板應用於XSLT 3.0中的序列.-關於添加/刪除:注意XSLT是一種聲明性語言,這些函數將生成一個新實例。 – 2011-04-27 20:49:36

+0

Rachel,也許你可以提供一個你想要的輸入/輸出類型的小例子?我問的原因是XSL通常不被用作過程語言,所以在迭代時更改列表中的值通常不是解決問題的理想方法。相反,你會指定你想要的最終結果是什麼,而不是如何得到結果。 – Erica 2011-04-28 01:32:25

+0

@Erica:你的觀點非常真實。我有興趣使用序列來實現解決方案,因爲我想了解更多信息。感謝您的答覆。 – Rachel 2011-04-29 01:28:51

回答

4

隨着澄清順序的一個實例,如在的XPath/XSLT別的是不可改變的,答案是肯定的

  1. 遍歷序列

    <xsl:for-each select="$seq"> 
    <!-- Whatever necessary code here --> 
    <!-- . is the current item of the sequence--> 
    </xsl:for-each> 
    
  2. Add an item to a sequence(產生一個新的序列,即此操作的結果):

    insert-before($target as item()*, 
           $position as xs:integer, 
           $inserts as item()*) as item()* 
    

摘要:返回從$ a的值構造一個新的序列定位 與 插入由值指定的位置$插入的值 $ position。 ($目標的值是 不受序列 結構。)

. 3. Concatenation of two sequences(產生一個新的序列,它是此操作的結果):

$seq1 , $seq2 

.。 4。 Remove an item from a sequence

 remove($target as item()*, $position as xs:integer) as item()* 

摘要:返回從$ a的值構造一個新的序列,在通過除去$位置 值指定的位置 與項定位

. 0.5。 Extract a subsequence from a sequence

subsequence($sourceSeq as item()*, 
       $startingLoc as xs:double, 
       $length as xs:double) **as item**()* 

摘要:返回 $ sourceSeq的貴重物品在由$ startingLoc 的值指示的位置 開始,一直持續的項目 數表示的連續序列由$ length的值。

而且有很多比較有用standard XPath 2.0 functions over sequences

注意:XPath 2.0序列沒有的唯一功能是「嵌套」。一個序列總是「平坦的」,而一個序列的一個項目本身不能是一個序列。有多種方法來模擬多級序列 - 例如,一個項目可以是一個節點,它的子節點可以被看作是一個嵌套序列。

更新:這裏是如何將這些功能可以方便地用於解決OP的更新問題:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:my="my:my" > 

<xsl:template match="/"> 
    <xsl:sequence select="my:populateSequence((), 1, 10)"/> 
</xsl:template> 

<xsl:function name="my:populateSequence" as="xs:integer*"> 
    <xsl:param name="pSeq" as="xs:integer*"/> 
    <xsl:param name="pStart" as="xs:integer"/> 
    <xsl:param name="pEnd" as="xs:integer"/> 

    <xsl:sequence select= 
    "if($pStart gt $pEnd) 
     then $pSeq 
     else my:populateSequence(($pSeq, $pStart), $pStart+1, $pEnd) 
    "/> 
</xsl:function> 
</xsl:stylesheet> 

當這個XSLT 2.0變換的任何XML文檔(未使用)應用,通緝結果產生

1 2 3 4 5 6 7 8 9 10 
+0

謝謝。它有助於 !! – Rachel 2011-04-28 14:57:41

+0

@Rachel:不客氣。 – 2011-04-28 15:06:43

+0

我很欣賞你的想法分享。我面臨着實現這個的語法問題。我已經用xsl更新了我的帖子。請提供您的意見以聲明序列中的空序列和插入項目。謝謝。 – Rachel 2011-04-28 23:18:17