2012-08-03 76 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