XSL - 列出等效实现

发布于 2024-11-03 20:42:22 字数 1710 浏览 0 评论 0原文

我想知道 XSL 2.0 中是否有类似于 Java 中的 List 的东西。我想递归调用模板 10 次并传递名为“mylist”的输入变量。在模板中,我想要执行诸如将项目添加到列表、从列表中删除项目、迭代列表中的项目等操作。我可以看到类似“序列”的内容,但我不确定它是否可以用于添加、删除、迭代等。请分享您的想法来实现这一点。

我尝试在以下响应的帮助下使用序列,我面临一些语法问题,例如声明空序列。我想使用 insert-before 或 concat 序列函数打印序列 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>

I would like to know if we have something in XSL 2.0, equivalent to a List in Java. I would like to recursively call a template 10 times and pass a input variable with name 'mylist'. Within the template, I want to do operations like adding item to list, removing item from list, iterating over items within the list etc. I could see something like 'sequence' but i am not sure if it can be used to add, remove, iterate etc. Please share your ideas to implement this.

I tried using sequence with the help of the below reponse, I face some issues with syntax, like declaring an empty sequence. I want to print the sequence 1 2 3 4 5 6 7 8 9 10, using the insert-before or concat sequnce functions. Please help me fix the syntax.

  <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-11-10 20:42:22

澄清序列的实例与 XPath/XSLT 中的其他任何内容一样都是不可变的,答案是肯定的

  1. 迭代序列

    
     
     <!--。是序列的当前项-->
    
    
  2. 将项目添加到序列(生成一个作为此操作结果的新序列):

     insert-before($target as item()*,
                     $position 作为 xs:integer,
                     $插入为 item()*) as item()*
    

摘要:返回根据 $target 的值构造的新序列
$inserts 的值插入于
由值指定的位置
$位置。 ($target 的值为
不受顺序影响
施工。)

.3. 两个序列的串联(产生一个新序列即此操作的结果):

   $seq1 , $seq2

..4。 从序列中删除项目

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

摘要:返回根据 $target 的值构造的新序列
与该位置的项目
由 $position 的值指定
已删除

..5。 从序列中提取子序列

 子序列($sourceSeq as item()*,
               $startingLoc 为 xs:double,
               $length as xs:double) **as item**()*

摘要:返回值中项目的连续序列
$sourceSeq 从该位置开始
由 $startingLoc 的值指示
并继续计算项目数量
由$length的值表示。

还有许多更有用的序列上的标准 XPath 2.0 函数

注意: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

With the clarification that an instance of a sequence, as anything else in XPath/XSLT are immutable, the answer is positive:

  1. Iterating over a sequence:

    <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 (produces a new sequence that is the result of this operation):

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

Summary: Returns a new sequence constructed from the value of $target
with the value of $inserts inserted at
the position specified by the value of
$position. (The value of $target is
not affected by the sequence
construction.)

.3. Concatenation of two sequences (produces a new sequence that is the result of this operation):

   $seq1 , $seq2

..4. Remove an item from a sequence:

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

Summary: Returns a new sequence constructed from the value of $target
with the item at the position
specified by the value of $position
removed

..5. Extract a subsequence from a sequence:

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

Summary: Returns the contiguous sequence of items in the value of
$sourceSeq beginning at the position
indicated by the value of $startingLoc
and continuing for the number of items
indicated by the value of $length.

And there are many more useful standard XPath 2.0 functions over sequences.

Note: The only feature that the XPath 2.0 sequence doesn't have is "nestedness". A sequence is always "flat" and an item of a sequence cannot be a sequence itself. There are ways to simulate multi-level sequences -- for example, an item can be a node and its children nodes can be regarded as a nested sequence.

Update: Here is how these functions can be used conveniently to solve the OP's updated question:

<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>

When this XSLT 2.0 transformation is applied on any XML document (not used), the wanted result is produced:

1 2 3 4 5 6 7 8 9 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文