4

我已经阅读了很多帖子并尝试了很多东西,但仍然无法让 xsl 在参数中找到值。我从 java 的 sun xalan 开始,但从未让它工作,所以我改用撒克逊语无济于事。我想用 xls 将两个 xml 文档合二为一。永远不会在文件系统上,这是用于构建 xml 字符串/文档的 Web 应用程序。我尝试将 DTMAxisIterator、DomSource、Doc 传递给在 xsl、字符串中设置的节点。它在带有 xsl document() 的 NotePad++ 中运行良好,但我不想将 xml 保存在系统上。

XSL

<xsl:param name="RsXml" select="/"/>

<xsl:template match="/policy/vehicles">
<vehicle type="DP" type_code="DP"/>

        <xsl:for-each select="$RsXml/InsuranceSvcRs /com.csc_PolicyOrderCurrentCarrierInqRs/PersVeh">

            <vin>
                <xsl:value-of select="VehIdentificationNumber"/>
            </vin>
            <veh_year>
                <xsl:value-of select="ModelYear"/>
            </veh_year>
            <make>
                <xsl:value-of select="Manufacturer"/>
            </make>
            <model>   
                <xsl:value-of select="Model"/>
            </model>
            <costnew>               
                <xsl:value-of select="CostNewAmt/Amt"/>
            </costnew>
            <symbol>
                <xsl:value-of select="VehSymbolCd"/>
            </symbol>
            <wheregaraged></wheregaraged>
            <liabilityonly></liabilityonly>
            <collision></collision>
            <comprehensive></comprehensive>
            <rentalreimbursement></rentalreimbursement>
            <towing></towing>
            <altered></altered>
            <title></title>
            <enginesize>
                <xsl:value-of select="NumCylinders"/>
            </enginesize>
            <trailertype/>
            <trtonnage/>
            <mctype/>
            <mcenginecc/>
            <vehicleuse></vehicleuse>
            <mhawnings></mhawnings>
            <vseat15></vseat15>
            <vseat15text/>
            <extraequipment></extraequipment>
            <mcsidecar></mcsidecar>
            <atvwheels/>
            <damage/>
            <endorsements/>
            <avtotal/>
            <v_underwriting>
                <altered></altered>
                <alteredlist/>
                <alteredexplain/>
                <businessuse></businessuse>
                <haulstudents></haulstudents>
                <pulltrailers></pulltrailers>
                <trailerendorsement/>
            </v_underwriting>
            <driverid></driverid>
            <gen_classcode></gen_classcode>
            <classcode></classcode>
            <primary_veh></primary_veh>
            <rates>
                <bi></bi>
                <pd></pd>
                <med></med>
                <ubi></ubi>
                <upd></upd>
                <comp></comp>
                <coll></coll>
                <comm></comm>
                <rr></rr>
                <tl></tl>
            </rates>
            <xferdis></xferdis>
            <atv_young_dr></atv_young_dr>
            <mrcd_date/>
            <hasdamage></hasdamage>
            <comp_symbol></comp_symbol>
            <str_legal></str_legal>
            <addresses/>


        </xsl:for-each>     
<xsl:apply-templates/>

XML一

           <?xml version="1.0" encoding="UTF-8"?>
               <policy id="1735">       
              <vehicles>        
              </vehicles>
                </policy>

XML 二

      <ACORD>
     <InsuranceSvcRs>
      <com.csc_PolicyOrderCurrentCarrierInqRs>
        <PersVeh id="001">
            <ItemIdInfo>
                <InsurerId>001</InsurerId>
            </ItemIdInfo>
            <Manufacturer>FORD</Manufacturer>
            <Model>WINDSTAR</Model>
            <ModelYear>1999</ModelYear>
            <VehBodyTypeCd>ES</VehBodyTypeCd>
            <CostNewAmt>
                <Amt>23660</Amt>
            </CostNewAmt>
            <NumDaysDrivenPerWeek />
            <EstimatedAnnualDistance>
                <NumUnits />
                <UnitMeasurementCd />
            </EstimatedAnnualDistance>
            <FullTermAmt>
                <Amt />
            </FullTermAmt>
            <TerritoryCd />
            <VehIdentificationNumber>1</VehIdentificationNumber>
            <NumCylinders>6</NumCylinders>
            <VehSymbolCd />
            <AntiLockBrakeCd>4-WHEEL STD</AntiLockBrakeCd>
            <DaytimeRunningLightInd />
            <DistanceOneWay>
                <NumUnits />
                <UnitMeasurementCd>MI</UnitMeasurementCd>
            </DistanceOneWay>
            <AntiTheftDeviceCd>PASS-KEY</AntiTheftDeviceCd>
            <VehPerformanceCd />
            <VehUseCd />
            <AirBagTypeCd>BOTH</AirBagTypeCd>
            <com.csc_VehBodyTypeFreeformInd />
        </PersVeh>          
    </com.csc_PolicyOrderCurrentCarrierInqRs>
    </InsuranceSvcRs>
   </ACORD>

班级

    public String transformResultXML(String xmlSource, Templates xsl,String policyXml ) {
       String result = "";  

    try {

          StringWriter writer = new StringWriter();
          StringReader reader2 = new StringReader(policyXml);

          XmlHelper xh = new XmlHelper();
          Document xmlSrc = xh.loadDoc(xmlSource);
          DOMSource source = new DOMSource(xmlSrc);


          ByteArrayInputStream byteStream = new  ByteArrayInputStream(xmlSource.getBytes());
          StringReader reader = new StringReader(xmlSource);
          SAXSource source2 = new SAXSource(new XMLFilterImpl(), new  InputSource(reader));

          TransformerFactory transFact = new com.icl.saxon.TransformerFactoryImpl();

          Transformer transformer = transFact.newTransformer();

          transformer.setParameter("RsXml",source2);
         // transformer.setParameter("RsXml",xmlSrc);
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          transformer.transform(new javax.xml.transform.stream.StreamSource(reader2),
                  new javax.xml.transform.stream.StreamResult(writer));
          result = writer.toString();
          System.out.println(result);

    } catch( Exception e ) {
        e.printStackTrace();
    }

    return result;
}   
4

3 回答 3

3

我能够让它与撒克逊人一起工作,见下面的代码。我认为关键是 document.getDocumentElement() as parm

      public String transformResultXML(String xmlSource, Templates xsl,String policyXml )    {
       String result = "";  

    try {

          StringWriter writer = new StringWriter();
          StringReader reader2 = new StringReader(policyXml);

          DocumentBuilderFactory dfactory =
                  DocumentBuilderFactory.newInstance( "com.icl.saxon.om.DocumentBuilderFactoryImpl",null);

          dfactory.setNamespaceAware(true);

          DocumentBuilder docBuilder = dfactory.newDocumentBuilder();

          org.w3c.dom.Document document = docBuilder.parse(new InputSource(new StringReader(xmlSource)));

          Transformer transformer = xsl.newTransformer();

          transformer.setParameter("RsXml", document.getDocumentElement());

          transformer.setOutputProperty(OutputKeys.INDENT, "yes");
          transformer.transform(new javax.xml.transform.stream.StreamSource(reader2),
                  new javax.xml.transform.stream.StreamResult(writer));
          result = writer.toString();
          System.out.println(result);

    } catch( Exception e ) {
        e.printStackTrace();
    }

XSL 片段

    <xsl:param name="RsXml" />

<xsl:template match="/policy/vehicles">


        <xsl:for-each select="$RsXml/InsuranceSvcRs/com.csc_PolicyOrderCurrentCarrierInqRs/PersVeh">
于 2012-11-14T20:42:44.950 回答
2

当您使用 JAXP 接口时,您可以为参数提供的值未在 API 规范中定义,并且 Saxon 的支持可能与 Xalan 的不同。一般来说,我想你会发现 Saxon 的 s9api 接口更容易使用。当然,它提供参数的方法是强类型的,你可以提供什么更清楚。如果你想提供一个节点,它应该是 XdmNode 的一个实例,你可以通过使用 s9api DocumentBuilder 解析词法 XML 或包装一个 DOM 节点来创建一个 XdmNode。

于 2012-12-05T17:32:36.617 回答
0

当您将参数传递给 XSL 样式表时,该值是一个字符串并且不会被解析为 DOM。目前这在标准 XSLT 中是不可能的。可能有一个扩展可以做到这一点,但我不知道有一个。

于 2012-11-14T19:56:16.923 回答