2012-11-14 72 views
3

我已經閱讀了大量文章並嘗試了很多東西,但仍然無法獲取xsl以在參數中查找值。我從java的sun xalan開始,從來沒有得到它的工作,所以我切換到撒克遜無濟於事。我想將兩個xml文檔合併成一個xls文檔。從來不在文件系統上,這是針對構建xml字符串/文檔的Web應用程序。我曾嘗試將DTMAxisIterator,DomSource,Doc傳遞給xsl,string中的節點集。它在NotePad ++中使用xsl文檔()工作正常,但我不想將xml保存在系統上。將xml文檔作爲參數傳遞給xsl

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

回答

2

我能夠得到它與撒克遜工作看到下面的代碼。我認爲,關鍵是document.getDocumentElement()作爲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"> 
+0

提供一個DOM作爲Saxon轉換的輸入可能意味着您的轉換運行速度慢10倍 - 這不被推薦。我會建議使用s9api接口。 –

0

將參數傳遞給XSL樣式表時,該值是一個字符串,不會被分析爲DOM。目前這在標準XSLT中是不可能的。可能有一個擴展可以做到這一點,但我不知道。

2

當您使用JAXP接口,可以提供對參數值未在API規範中定義Saxon的支持可能與Xalan不同。一般來說,我認爲你會發現撒克遜的s9api界面更容易使用。當然,它提供參數的方法是強類型的,它可以提供更清晰的內容。如果您想提供一個節點,它應該是XdmNode的一個實例,並且您可以通過使用s9api DocumentBuilder解析詞法XML或通過包裝DOM節點來創建XdmNode。

+0

感謝您的信息。 –

相關問題