0

我已经使用 XSLT 多年,但从未遇到过 SOAP 包装的 XML 文件。

我需要使用下面的 XSLT 修改 XML 消息,以将一些特定元素而不是整个正文提取到新的 XML 文件中。我已经声明了命名空间,但无论我如何设置 xsl:template 匹配,我似乎都无法引用 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>
4

1 回答 1

0

正如Jim Garrison 所提到的,您曾经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。

于 2012-08-03T19:15:35.753 回答