简体   繁体   English

XSLT 如何使用 XALAN 迭代 Java 列表

[英]XSLT How to iterate a Java List with XALAN

I am trying to iterate over an array using XSLT in order to get a final text looking like this:我正在尝试使用 XSLT 遍历数组以获得如下所示的最终文本:

array1.firstElement

array1.secondElement

I have this in my XSLT:我的 XSLT 中有这个:

<xsl:param name="nombreEmpresaDebe" />

And I put this in my transform method:我把它放在我的转换方法中:

transformer.setParameter("nombreEmpresaDebe", listaNombreEmpresaDebe);

renglones/reglon is the name of my node and I am already iterating over it with the following code: renglones/reglon是我的节点的名称,我已经使用以下代码对其进行了迭代:

<xsl:for-each select='renglones/renglon'>
    <label>
        <xsl:value-of select="rubro" />
    </label>

    <label>
        <xsl:value-of select="$nombreEmpresaDebe"/>
    </label>
<xsl:for-each>

Now I have to get $nombreEmpresaDebe elements which is a java.util.List but I have no clue about doing it in the same for-each.现在我必须获得$nombreEmpresaDebe元素,它是一个java.util.List但我不知道在相同的 for-each 中执行它。 Does anyone know how to do it?有谁知道怎么做?

I need something like this:我需要这样的东西:

 <renglon1>
    <rubro>rubro1</rubro>
    <nombreEmpresaDebe>firstElement</nombreEmpresaDebe>
 </renglon1>
 <renglon2>
    <rubro>rubro2</rubro>
    <nombreEmpresaDebe>secondElement</nombreEmpresaDebe>
 </renglon2>

You can accomplish this by calling a recursive template.您可以通过调用递归模板来完成此操作。 For example例如

<xsl:call-template name="outputList">
    <xsl:with-param name="list" select="$nombreEmpresaDebe"/>
    <xsl:with-param name="index" select="0"/>
</xsl:call-template>

Where the template is defined like模板定义如下

<xsl:template name="outputList">
    <xsl:param name="list"/>
    <xsl:param name="index"/>
        
    <xsl:if test="number($index) &lt; java:size($list)">
         <label>
             <xsl:value-of select="$nombreEmpresaDebe"/>
         </label>
         <xsl:call-template name="outputList">
             <xsl:with-param name="list" select="$list"/>
             <xsl:with-param name="index" select="number($index)+1"/>
         </xsl:call-template>
     </xsl:if>
</xsl:template>

I should note that my answer does not combine this with the iteration of the other nodelist you are handling, but this is the basic List iterating technique that could be combined with that.我应该注意,我的回答没有将它与您正在处理的其他节点列表的迭代相结合,但这是可以与之结合的基本 List 迭代技术。 Also, I realize this is 3 years late, but someone else my find this question and benefit from having an answer.另外,我意识到这已经晚了 3 年,但其他人我发现了这个问题并从答案中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM