简体   繁体   English

XSLT 2.0-遍历Java对象列表

[英]XSLT 2.0 - iterate over a list of Java objects

I have an XSL that is meant to create a copy of an XML but with a few attributes added to certain elements in the XML. 我有一个XSL,用于创建XML的副本,但向XML中的某些元素添加了一些属性。 The XSL calls a Java function that returns a list (java.util.List) of objects of type CInfo which is currently a very simple class defined as follows: XSL调用一个Java函数,该函数返回CInfo类型的对象的列表(java.util.List),该对象当前是一个非常简单的类,定义如下:

public class CInfo {
    public int getNewVal() {
        return 12345;
    }
}

I am now facing a problem with the following snippet in the XSL: 我现在在XSL中遇到以下代码段的问题:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:list="java.util.List"
    xmlns:saxon="http://saxon.sf.net/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    ...
    ...>

...
<xsl:variable name="infoList"
    select="up:computeUpdates($updator)"/>

<xsl:for-each select="$infoList">
    <xsl:variable name="cinfo" select="."/>
    <xsl:variable name="newVal" select="ci:getNewVal($cinfo)"/>
...
...
</xsl:for-each>

computeUpdates() , which I verified is indeed being called by XSL, returns a list containing just one instance of type CInfo. 我验证了XSL确实调用了computeUpdates() ,它返回仅包含一个CInfo类型实例的列表。 The problem occurs at xsl:for-each which gives the following error: 该问题发生在xsl:for-each处 ,该错误给出以下错误:

Error on line 89
  XPTY0019: Required item type of first operand of '/' is node(); supplied value has item
  type java:com.mproj.mpkg.CInfo
  at xsl:for-each (#76)
     processing "com.mproj.mpkg.."
  at xsl:apply-templates (#48)

Somehow the XSL doesn't seem to be able to iterate over infoList . XSL似乎无法以某种方式遍历infoList Strangely, an existing XSL in the codebase that I am working on has a very similar for-each and is able to iterate over a list of Java objects of another class (similar to CInfo though) and doesn't seem to give any problem. 奇怪的是,我正在开发的代码库中的现有XSL具有非常相似的for-each ,并且能够遍历另一个类的Java对象列表(尽管类似于CInfo),并且似乎没有任何问题。 Am I missing something? 我想念什么吗? What is the standard procedure to make an XSLT iterate over a list of Java objects? 使XSLT遍历Java对象列表的标准过程是什么? Any examples that can help me here? 有什么例子可以帮助我吗? I tried searching the web for examples of such looping and possible solutions to the mentioned problem but have been unsuccessful so far. 我尝试在网络上搜索此类循环的示例以及针对上述问题的可能解决方案,但到目前为止仍未成功。

You haven't shown us line 89, where the error arises. 您尚未向我们显示出现错误的第89行。 My guess is that it probably contains an expression of the form 我的猜测是它可能包含以下形式的表达式

$infoList/XXXXX $ infoList / XXXXX

which is failing because (as the error message explains) the lh operand of "/" must be a sequence of nodes. 之所以失败,是因为(如错误消息所说明的)“ /”的lh操作数必须是节点序列。

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

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