2012-08-03 108 views
0

我已經使用了XSLT多年,但從未遇到過SOAP封裝的XML文件。使用XSLT從SOAP抽取XML使用XSLT抽取XML

我需要使用下面的XSLT修改XML消息來提取一些特定的元素,而不是整個身體到一個新的XML文件。我已經聲明瞭命名空間,但無論如何設置xsl:模板匹配,我似乎都無法引用Batch元素下的任何內容。

任何人都可以點我正確的方向請。

砍倒XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:header> 
    <div:delivery xmlns:div="http://schemas.biztalk.org/btf-2-0/delivery" SOAP- ENV:mustUnderstand="1"> 
    <div:message> 
    <div:messageID></div:messageID> 
    </div:message> 
    <div:to> 
    <div:address></div:address> 
    </div:to> 
    <div:from> 
     <div:address></div:address> 
    </div:from> 
    </div:delivery> 
    <prop:properties xmlns:prop="http://schemas.biztalk.org/btf-2-0/properties" SOAP-ENV:mustUnderstand="1"> 
    <prop:identity>00000000</prop:identity> 
    <prop:sentAt>2012-06-27T20:00:57</prop:sentAt> 
    <prop:expiresAt>2010-01-01T00:00:00</prop:expiresAt> 
    <prop:topic>BBAINV</prop:topic> 
    </prop:properties> 
</SOAP-ENV:header> 
<SOAP-ENV:body> 
<Batch xmlns="urn:schemas-basda-org:BatchEnvelope" DocType="Invoice" SupplierName="76787980" Number="2" Date="2012-06-27"> 
<Invoice> 
<InvoiceHead> 
<Schema> 
<Version>3.09</Version> 
</Schema> 
<Stylesheet> 
<StylesheetOwner>BASDA</StylesheetOwner> 
<StylesheetName>eBIS-XML_simple.xsl</StylesheetName> 
<Version>1</Version> 
<StylesheetType>XSL</StylesheetType> 
<LogoURL>http://www.someone.co.uk/stylesheets/invoices.xsl</LogoURL> 
</Stylesheet> 
<Parameters> 
<Language>en-GB</Language> 
<DecimalSeparator>.</DecimalSeparator> 
<Precision>20.4</Precision> 
</Parameters> 
<OriginatingSoftware> 
<SoftwareManufacturer></SoftwareManufacturer> 
<SoftwareProduct></SoftwareProduct> 
<SoftwareVersion></SoftwareVersion> 
<SoftwareSystemID></SoftwareSystemID> 
</OriginatingSoftware> 
<TestFlag TestReference=""> 
<Test ExpectedReply="" Mode=""></Test> 
</TestFlag> 
<InvoiceType Code="INV" Codelist="99">INVOICE</InvoiceType> 
<Function Code="" Codelist="" Preserve=""></Function> 
<InvoiceCurrency> 
<Currency Code="GBP" Codelist="">GBP</Currency> 
<AlternateCurrency Code="" Codelist=""></AlternateCurrency> 
<Rate CalculationType=""></Rate> 
</InvoiceCurrency> 
<Intrastat> 
<CommodityCode></CommodityCode> 
<CommodityDescription></CommodityDescription> 
<NatureOfTransaction Code="" Codelist=""></NatureOfTransaction> 
<SupplementaryUnits></SupplementaryUnits> 
<CountryOfDestination Code="" Codelist=""></CountryOfDestination> 
<ModeOfTransport Code="" Codelist=""></ModeOfTransport> 
</Intrastat> 
<Checksum></Checksum> 
</InvoiceHead> 
<InvoiceReferences> 
<ContractOrderReference>1499001</ContractOrderReference> 
<CostCentre>999925/1</CostCentre> 
<BuyersOrderNumber Preserve="">22111366</BuyersOrderNumber> 
<Department></Department> 
<GeneralLedgerCode></GeneralLedgerCode> 
<ProjectCode Preserve="true">SH146597</ProjectCode> 
<ProjectAnalysisCode Preserve="true">101999</ProjectAnalysisCode> 
<SuppliersInvoiceNumber Preserve="true">10011999</SuppliersInvoiceNumber> 
<DeliveryNoteNumber>SH 22925/1</DeliveryNoteNumber> 
<SuppliersOrderReference Preserve="">SH 111925</SuppliersOrderReference> 
<ResponseTo></ResponseTo> 
<BatchNumber></BatchNumber> 
<GloballyUniqueID Preserve=""></GloballyUniqueID> 
</InvoiceReferences> 
</Invoice> 
</Batch> 

完整的樣式表

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:b="urn:schemas-basda-org:BatchEnvelope"> 

<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/> 

<xsl:variable name="newline"> 
<xsl:text> 
</xsl:text> 
</xsl:variable> 

<!-- *******************************************************************************--> 
<xsl:template match="/"> 
    <xsl:apply-templates select="b:Batch"/> 
</xsl:template> 

<xsl:template match="/b:Batch"> 
    <Batch> 
    <xsl:value-of select="$newline"/><!-- Put in a line break --> 
    <xsl:for-each select="b:Invoice"> 
    <Invoice> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <InvoiceHead> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceHead/InvoiceType"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </InvoiceHead> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="TaxPointDate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="OriginalOrderDate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <InvoiceReferences> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="InvoiceReferences/DeliveryNoteNumber"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="InvoiceReferences/SuppliersOrderReference"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="InvoiceReferences/BuyersOrderNumber"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="InvoiceReferences/SuppliersInvoiceNumber"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </InvoiceReferences> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceDate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <Supplier> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <SupplierReferences> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Supplier/SupplierReferences/BuyersCodeForSupplier"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </SupplierReferences> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </Supplier> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <Buyer> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <BuyerReferences> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Buyer/BuyerReferences/Organisation"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </BuyerReferences> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="Buyer/Party"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="Buyer/Address"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <Contact> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Buyer/Contact/DDI"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </Contact> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </Buyer> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <Delivery> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <DeliverTo> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Delivery/DeliverTo/Address"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </DeliverTo> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="Delivery/DeliveryInformation"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <DeliverFrom> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <DeliverFromReferences> 
        <xsl:value-of select="$newline"/><!-- Put in a line break --> 
        <xsl:copy-of select="Delivery/DeliverFrom/DeliverFromReferences/SuppliersCodeForLocation"/> 
        <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       </DeliverFromReferences> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Delivery/DeliverFrom/Party"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Delivery/DeliverFrom/Address"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <Contact> 
        <xsl:value-of select="$newline"/><!-- Put in a line break --> 
        <xsl:copy-of select="Delivery/DeliverFrom/Contact/DDI"/> 
        <xsl:value-of select="$newline"/><!-- Put in a line break --> 
        <xsl:copy-of select="Delivery/DeliverFrom/Contact/Fax"/> 
        <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       </Contact> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </DeliverFrom> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </Delivery> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:for-each select="InvoiceLine"> 
     <InvoiceLine> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <Product> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Product/SuppliersProductCode"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Product/Description"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </Product> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="Narrative"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <Quantity> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Quantity/Amount"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </Quantity> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="Quantity"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="GrossLineTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <Price> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Price/UnitPrice"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="Price/SpecialPriceDescription"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </Price> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceLineInformation"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <LineTax> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="LineTax/TaxRate"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
       <xsl:copy-of select="LineTax/TaxValue"/> 
       <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      </LineTax> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="LineTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </InvoiceLine> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </xsl:for-each> 

     <xsl:copy-of select="Narrative"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <TaxSubTotal> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="TaxSubTotal/TaxRate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="TaxSubTotal/NumberOfLinesAtRate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="TaxSubTotal/TaxableValueAtRate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break -->  
      <xsl:copy-of select="TaxSubTotal/TaxAtRate"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </TaxSubTotal> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <InvoiceTotal> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/NumberOfLines"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/NumberOfTaxRates"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/FreightCharges"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/LineValueTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/TaxableTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/TaxableTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/TaxTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
      <xsl:copy-of select="InvoiceTotal/GrossPaymentTotal"/> 
      <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </InvoiceTotal> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
    </Invoice> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     </xsl:for-each> 
    <TaxBatchTrailer> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/TaxRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/TotalValueAtRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/TaxableValueAtRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/TaxAtRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/NetPaymentAtRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="TaxBatchTrailer/GrossPaymentAtRate"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
    </TaxBatchTrailer> 
    <xsl:value-of select="$newline"/><!-- Put in a line break --> 
    <BatchTrailer> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/TotalNumberOfItems"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/TotalValue"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/TaxableTotal"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/TaxTotal"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/NetPaymentTotal"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
     <xsl:copy-of select="BatchTrailer/GrossPaymentTotal"/> 
     <xsl:value-of select="$newline"/><!-- Put in a line break --> 
    </BatchTrailer> 
    <xsl:value-of select="$newline"/><!-- Put in a line break --> 
</Batch> 
<xsl:value-of select="$newline"/><!-- Put in a line break --> 
</xsl:template> 
</xsl:stylesheet> 
+1

'D:Batch'!='B:Batch' - 這是一個錯字嗎? – 2012-08-03 18:25:37

回答

0

正如吉姆·加里森提到的,你有d:batch(即使用 'd' 前綴,這還沒有被宣佈,而不是' b')。那將是一個致命的錯誤,所以你可能在你的非縮減樣式表中沒有真正的這個錯誤。

另一個會阻止匹配Batch元素的問題是您要求頂級元素「/ b:Batch」,而在示例輸入中,<Batch>不是頂級元素。但是,再次,也許這是您的裁減XSLT和實際樣式表之間的區別。 (請確保您的簡裝樣本代表真實的東西好!)

如果你在訪問<Batch>但在訪問該是<Batch>後代元素沒有成功實際上成功的,問題是,你不解決這些元素在他們的名字空間中。

xmlns="..."<Batch>上的默認命名空間聲明被其所有後代(直到被覆蓋)繼承。因此,沒有名稱空間前綴的任何後代(例如<Invoice>)都位於相同的默認名稱空間「urn:schemas-basda ...」中。因此,而不是

<xsl:for-each select="Invoice"> 

你需要

<xsl:for-each select="b:Invoice"> 

HTH。

+0

感謝您的評論,XSLT中的拼寫錯誤是我的錯,所以我添加了完整的sytlesheet,示例XML是準確的。如果我將該行更改爲我仍然沒有輸出。所以我認爲我仍然沒有訪問正確的後代:( – Simonhi99 2012-08-06 09:17:05

+0

@ Simonhi99,請參閱我的答案中的第2段。樣式表仍然不起作用,因爲您試圖將模板應用於'「/ b:Batch」',即a頂層元素,但頂層沒有''將你的第一個應用模板改爲'select =「// b:Batch」'並將你的第二個模板匹配模式改爲'match =「b:批處理「'(沒有斜線) – LarsH 2012-08-06 15:09:25

+0

非常感謝您的協助,這一切都非常完美。 – Simonhi99 2012-08-07 10:18:01