使用填充了数据库值的java列表来迭代xsl转换并生成多个xml

时间:2016-03-02 07:21:38

标签: java xml xslt saxon

我在xslt中使用自定义xpath函数调用,其中my方法返回从数据库中获取的对象列表。我必须根据我从数据库获得的结果集的数量生成多个xml。

导致异常:

Error on line 28 of comptel.xsl:
  XPTY0020: Required item type of the context item for the child axis is node(); actual type
  is xs:string
  in built-in template rule
Exception during transformation
; SystemID: file:/F:/OMFOLDER/comptel.xsl; Line#: 28; Column#: -1
net.sf.saxon.trans.XPathException: Required item type of the context item for the child axis is node(); actual type is xs:string
    at com.saxonica.ee.bytecode.util.Callback.makeXPathException(Callback.java:143)
    at EE_OrderSubline_5215127407.process(file:/F:/OMFOLDER/comptel.xsl:79)
    at com.saxonica.ee.bytecode.CompiledExpression.process(CompiledExpression.java:69)
    at net.sf.saxon.expr.instruct.TemplateRule.applyLeavingTail(TemplateRule.java:395)
    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:456)
    at net.sf.saxon.trans.TextOnlyCopyRuleSet.process(TextOnlyCopyRuleSet.java:65)
    at net.sf.saxon.trans.Mode.applyTemplates(Mode.java:433)
    at net.sf.saxon.Controller.transformDocument(Controller.java:2261)
    at net.sf.saxon.Controller.transform(Controller.java:1863)
    at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:553)
    at net.sf.saxon.jaxp.TransformerImpl.transform(TransformerImpl.java:183)
    at com.thbs.provision.XSLTSaxonica.transformSaxonmain(XSLTSaxonica.java:212)
    at com.thbs.provision.JPAServlet.service(JPAServlet.java:43)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3363)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)

这是我的Java类

public static List<String> assignDbvalue()
    {
        try {
            conn = ds.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql);

            ResultSet rs=null;

            rs = ps.executeQuery();
            ResultSetMetaData metadata = rs.getMetaData();
            int numberOfColumns = metadata.getColumnCount();
            System.out.println("numberOfColumns "+numberOfColumns);
            for(int i=1;i<=numberOfColumns;i++)
            {
            while (rs.next()) {              

                    arrayList.add(rs.getString(i));
                    System.out.println(rs.getString(i));
                }
            }
        return arrayList;

    }

这是我的xslt:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:sql="http://ns.saxonica.com/sql"
 xmlns:nd="java:com.thbs.provision.XSLTSaxonica"
 xmlns:exslt="http://exslt.org/common"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:java="http://saxon.sf.net/java-type"
 xmlns:saxon="http://saxon.sf.net/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 exclude-result-prefixes="java saxon xsd xsi xsl sql"
 extension-element-prefixes="saxon sql"
 >
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"
/>


<xsl:template match="OrderSubline">


<xsl:for-each select="exslt:node-set(nd:assignDbvalue())">

<ModifyRequest  xmlns="http://soa.comptel.com/2011/02/instantlink">
<RequestHeader>


 <NeType>BST</NeType>
<OrderNo><xsl:value-of select="OrderNumber/text()"/></OrderNo>
 <ReqUser><xsl:value-of select="Source/text()"/></ReqUser>
</RequestHeader>
<RequestParameters>

<Parameter>
<xsl:attribute name="name">REQ_OBJ</xsl:attribute>
<xsl:attribute name="value">1</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">REQ_TYPE</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="OrderSublineType/text()"/>
</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">IL_REQ_GROUP</xsl:attribute>
<xsl:attribute name="value">BHARTI</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">ACT_CODE1</xsl:attribute>
<xsl:attribute name="value">
 <xsl:value-of select="." />
</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">RESP_QUEUE_ID</xsl:attribute>
<xsl:attribute name="value">SYNCSOA</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">MSISDN</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="MobileNumber/text()"/>
</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">CIRCLE_ID</xsl:attribute>
<xsl:attribute name="value">
 <xsl:value-of select="Circle/text()"/>
</xsl:attribute>
</Parameter>

<Parameter>
<xsl:attribute name="name">PROD_ID_1</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="ProductItem/ProductItemId/text()"/>
</xsl:attribute>
</Parameter>

</RequestParameters> 
</ModifyRequest>
 </xsl:for-each>   
</xsl:template>

</xsl:stylesheet>

请提示我是否遗漏了某些东西或是否有其他方法可以实现它。

1 个答案:

答案 0 :(得分:0)

您的扩展功能正在返回一个List,然后您将exsl:node-set()应用于它。也许你假设这将把字符串变成节点?在Saxon中,exsl:node-set()是一个标识函数 - 它返回其参数不变。所以你使用xsl:for-each迭代一系列字符串,然后使用上下文项作为字符串,你使用像for (Map<String, Integer> item : Items) { for (Map.Entry<String, Integer> entry: items.get(i).entrySet()) { // do what you want with the entry } } 这样的表达式,除非上下文项是一个节点,否则它不起作用。

如果扩展函数返回的字符串是词法XML的片段,则使用select="OrderNumber/text()函数将它们转换为节点,而不是parse-xml()

(注意:我认为有一些exsl:node-set()的实现,如果你提供一个字符串,他们会将字符串解析为词汇XML。但是,{的规范中没有任何内容。 {3}}鼓励这种解释。)