我想创建用于复制相同输入XML的XSLT,但希望更改一些具有静态值的XML标记。
我尝试过自行构建XSLT,但我可以看到输出XML与输入XML相同。
这是输入XML文件
<?xml version="1.0" encoding="utf-8"?>
<Document
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
SystemVersion="7.00.3.71.03"
System="HubBroker"
Version="101"
xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01">
<Header>
<SenderEndpointID qualifier="CVR">00000000</SenderEndpointID>
<ReceiverEndpointID qualifier="EAN">6564654656546</ReceiverEndpointID>
<CreatedDate>2016-04-07</CreatedDate>
<CreatedTime>12:25:59</CreatedTime>
<EDIRefNo>24721</EDIRefNo>
<Test>false</Test>
<AcknowledgementRequest>Yes</AcknowledgementRequest>
<MailBounce>false</MailBounce>
<AgreementNo>0000</AgreementNo>
<InternalMessage>false</InternalMessage>
</Header>
<Body>
<OriginalReceiverEndpointID>0000</OriginalReceiverEndpointID>
<ReferencedDocumentType>INVOIC</ReferencedDocumentType>
<DispatchReference>28373</DispatchReference>
<DocumentType>INVOIC</DocumentType>
<DocumentNo>24721</DocumentNo>
<ReceiptStatus>false</ReceiptStatus>
<ResponseIndicator>12</ResponseIndicator>
<ReferencedInvoiceNo />
<ReferencedCreditNoteNo />
<DocumentDate>2016-04-07</DocumentDate>
<StartDateCatalogue>2016-04-07</StartDateCatalogue>
<EndDateCatalogue>2016-04-07</EndDateCatalogue>
<ShipmentDate>2016-04-07</ShipmentDate>
<DueDate>2016-05-30</DueDate>
<InvoiceDate>2016-04-07</InvoiceDate>
<PmtDiscountDate>2016-04-07</PmtDiscountDate>
<RequestedDeliveryDate>2016-04-07</RequestedDeliveryDate>
<PromisedDeliveryDate>2016-04-07</PromisedDeliveryDate>
<PromisedDeliveryTime>00:00:00</PromisedDeliveryTime>
<OrderDate>2016-04-07</OrderDate>
<QuoteNo>0000</QuoteNo>
<CampaignNo>0000</CampaignNo>
<AccountCode>KONTOOVERFØRSEL</AccountCode>
<OrderNo>PO5101903</OrderNo>
<BankName>Danske Bank</BankName>
<BankBranchNo>4394</BankBranchNo>
<BankAccountNo>4795026355</BankAccountNo>
<IBAN />
<SWIFT />
<PaymentType />
<PaymentID>4795026355</PaymentID>
<PaymentVendorNo />
<PaymentTermsCode>KONTOOVERFØRSEL</PaymentTermsCode>
<CurrencyCode>DKK</CurrencyCode>
<ShipmentNo>28373</ShipmentNo>
<ContractNo>0000</ContractNo>
<PriceListNo>0000</PriceListNo>
<ExternalDocumentNo>PO5101903</ExternalDocumentNo>
<PayableAmount>9756.25</PayableAmount>
<TaxableAmount>7805.00</TaxableAmount>
<TaxAmount>1951.25</TaxAmount>
<TaxInclusiveAmount>9756.25</TaxInclusiveAmount>
<TotalItemAmount>7805.00</TotalItemAmount>
<TotalDiscountAmount>0</TotalDiscountAmount>
<TotalChargeAmount>0</TotalChargeAmount>
<PaymentDiscountPercent>0.0</PaymentDiscountPercent>
<NetTotal>0.0</NetTotal>
<ShipmentMethodCode>ABLAGER</ShipmentMethodCode>
<SeasonText>0000</SeasonText>
<Collection>0000</Collection>
<CatalogueFunctionCode>9</CatalogueFunctionCode>
<Comments>
<Comment>Disp.: h914hje</Comment>
</Comments>
<FreeText>
<Text no="DEL">
<TextString>Disp.: h914hje</TextString>
</Text>
</FreeText>
<References>
<Ref type="ON">
<Reference>PO5101903</Reference>
</Ref>
</References>
<YourReference />
<Rounding>0.0</Rounding>
<CollectionDate>2016-04-07</CollectionDate>
<CollectionTime>00:00:00</CollectionTime>
<LoadIndicator>2</LoadIndicator>
<TransportPriority>3</TransportPriority>
<TransportRefNo />
<TransportPmtIndicator />
<TransportMethodCode>1</TransportMethodCode>
<Charges>
<Charge type="FREIGHT">
<Amount>0.00</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</Charge>
</Charges>
<Allowances>
<Allowance type="Standard">
<Amount>0.00</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</Allowance>
</Allowances>
<ExciseDuties>
<ExciseDuty type="9">
<Amount>0.0</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</ExciseDuty>
</ExciseDuties>
<InvoiceDiscounts>
<InvoiceDiscount DiscountType="3">
<DiscountPeriodUnit>Day</DiscountPeriodUnit>
<DiscountPeriod />
<DiscountPercent />
</InvoiceDiscount>
</InvoiceDiscounts>
<Parties>
<Party type="Billto">
<No>6564654656546</No>
</Party>
<Party type="Sellto">
<No>6564654656546</No>
<Name>Denmark Danmark A/S</Name>
<Name2 />
<Postbox />
<Address>Anelystparken 16</Address>
<Address2 />
<BuildingNumber />
<City>Mundelstrup</City>
<PostCode>8381</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo />
<Contact>Ikke angivet</Contact>
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
<Party type="Supplier">
<No>00000000</No>
<Name>asd asd A/S</Name>
<Name2 />
<Postbox />
<Address>Gejlhavegård 17</Address>
<Address2 />
<BuildingNumber />
<City>Kolding</City>
<PostCode>6000</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo>
DK00000000
</VATRegNo>
<Contact />
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
<Party type="Shipto">
<No>19555305</No>
<Name />
<Name2 />
<Postbox />
<Address>
Denmark Hjørring
Frederikshavnsvej 290
</Address>
<Address2 />
<BuildingNumber />
<City>Hjørring</City>
<PostCode>9800</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo />
<Contact />
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
</Parties>
<TaxSubtotals>
<TaxSubtotal type="S">
<TaxableAmount>7805.00</TaxableAmount>
<TaxAmount>1951.25</TaxAmount>
<TaxCategory>
<ID>S</ID>
<Percent>25.00</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxSubtotals>
<Lines>
<Line>
<LineNo>1</LineNo>
<LineRefNo>1</LineRefNo>
<EANNo>0200100218620</EANNo>
<ItemNo>OL 0308 B</ItemNo>
<RequestNotification>3</RequestNotification>
<SublineReference>1</SublineReference>
<RequestedDeliveryDate>2016-04-07</RequestedDeliveryDate>
<PromisedDeliveryDate>2016-04-07</PromisedDeliveryDate>
<PlannedDeliveryDate>2016-04-07</PlannedDeliveryDate>
<PlannedShipmentDate>2016-04-07</PlannedShipmentDate>
<ShipmentDate>2016-04-07</ShipmentDate>
<EarliestDeliveryDate>2016-04-07</EarliestDeliveryDate>
<LatestDeliveryDate>2016-04-07</LatestDeliveryDate>
<ExpiryDate>2016-04-07</ExpiryDate>
<Description>Stålvask 400 x 340 mm</Description>
<GrossWeight>0</GrossWeight>
<NetWeight>0</NetWeight>
<PacketType>Box</PacketType>
<UnitsperParcel>0</UnitsperParcel>
<UnitVolume>0</UnitVolume>
<UnitCost>0</UnitCost>
<Quantity>5.00</Quantity>
<OutstandingQuantity>0</OutstandingQuantity>
<QuantityShipped>0</QuantityShipped>
<MinimumQuantity>0</MinimumQuantity>
<CreditedQuantity>0</CreditedQuantity>
<QuantityInvoiced>5.00</QuantityInvoiced>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>198.00</UnitPrice>
<NetUnitPrice>198.00</NetUnitPrice>
<Amount>990.00</Amount>
<AmountIncludingVAT>1237.50</AmountIncludingVAT>
<LineExtensionAmount>990.00</LineExtensionAmount>
<InvDiscAmounttoInvoice>0</InvDiscAmounttoInvoice>
<RetailPrice>0</RetailPrice>
<TaxSubtotals>
<TaxSubtotal type="S">
<TaxableAmount>990.00</TaxableAmount>
<TaxAmount>247.50</TaxAmount>
<TaxCategory>
<ID>S</ID>
<Percent>25.00</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxSubtotals>
<LineDiscountPct>0</LineDiscountPct>
<LineDiscountAmount>0.00</LineDiscountAmount>
</Line>
</Lines>
</Body>
</Document>
我想生成相同的输出文件,但很少有标签使用静态值更新。
这是我试图制作的xslt代码,
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Header">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:element name="SenderEndpointID">11111111</xsl:element>
</xsl:copy>
</xsl:template>
<!--Identity template, provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我想更新Header标记中的SenderEndpointID,其值为'11111111'
<No>
内的<VATRegNo>
和<Party type="Supplier">
标记 - 其他标记将保持不变。
<Party type="Supplier">
<No>11111111</No>
<VATRegNo>DK11111111</VATRegNo>
</Party>
所以最终输出应该看起来像
<?xml version="1.0" encoding="utf-8"?>
<Document
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
SystemVersion="7.00.3.71.03"
System="HubBroker"
Version="101"
xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01">
<Header>
<SenderEndpointID qualifier="CVR">11111111</SenderEndpointID>
<ReceiverEndpointID qualifier="EAN">6564654656546</ReceiverEndpointID>
<CreatedDate>2016-04-07</CreatedDate>
<CreatedTime>12:25:59</CreatedTime>
<EDIRefNo>24721</EDIRefNo>
<Test>false</Test>
<AcknowledgementRequest>Yes</AcknowledgementRequest>
<MailBounce>false</MailBounce>
<AgreementNo>0000</AgreementNo>
<InternalMessage>false</InternalMessage>
</Header>
<Body>
<OriginalReceiverEndpointID>0000</OriginalReceiverEndpointID>
<ReferencedDocumentType>INVOIC</ReferencedDocumentType>
<DispatchReference>28373</DispatchReference>
<DocumentType>INVOIC</DocumentType>
<DocumentNo>24721</DocumentNo>
<ReceiptStatus>false</ReceiptStatus>
<ResponseIndicator>12</ResponseIndicator>
<ReferencedInvoiceNo />
<ReferencedCreditNoteNo />
<DocumentDate>2016-04-07</DocumentDate>
<StartDateCatalogue>2016-04-07</StartDateCatalogue>
<EndDateCatalogue>2016-04-07</EndDateCatalogue>
<ShipmentDate>2016-04-07</ShipmentDate>
<DueDate>2016-05-30</DueDate>
<InvoiceDate>2016-04-07</InvoiceDate>
<PmtDiscountDate>2016-04-07</PmtDiscountDate>
<RequestedDeliveryDate>2016-04-07</RequestedDeliveryDate>
<PromisedDeliveryDate>2016-04-07</PromisedDeliveryDate>
<PromisedDeliveryTime>00:00:00</PromisedDeliveryTime>
<OrderDate>2016-04-07</OrderDate>
<QuoteNo>0000</QuoteNo>
<CampaignNo>0000</CampaignNo>
<AccountCode>KONTOOVERFØRSEL</AccountCode>
<OrderNo>PO5101903</OrderNo>
<BankName>Danske Bank</BankName>
<BankBranchNo>4394</BankBranchNo>
<BankAccountNo>4795026355</BankAccountNo>
<IBAN />
<SWIFT />
<PaymentType />
<PaymentID>4795026355</PaymentID>
<PaymentVendorNo />
<PaymentTermsCode>KONTOOVERFØRSEL</PaymentTermsCode>
<CurrencyCode>DKK</CurrencyCode>
<ShipmentNo>28373</ShipmentNo>
<ContractNo>0000</ContractNo>
<PriceListNo>0000</PriceListNo>
<ExternalDocumentNo>PO5101903</ExternalDocumentNo>
<PayableAmount>9756.25</PayableAmount>
<TaxableAmount>7805.00</TaxableAmount>
<TaxAmount>1951.25</TaxAmount>
<TaxInclusiveAmount>9756.25</TaxInclusiveAmount>
<TotalItemAmount>7805.00</TotalItemAmount>
<TotalDiscountAmount>0</TotalDiscountAmount>
<TotalChargeAmount>0</TotalChargeAmount>
<PaymentDiscountPercent>0.0</PaymentDiscountPercent>
<NetTotal>0.0</NetTotal>
<ShipmentMethodCode>ABLAGER</ShipmentMethodCode>
<SeasonText>0000</SeasonText>
<Collection>0000</Collection>
<CatalogueFunctionCode>9</CatalogueFunctionCode>
<Comments>
<Comment>Disp.: h914hje</Comment>
</Comments>
<FreeText>
<Text no="DEL">
<TextString>Disp.: h914hje</TextString>
</Text>
</FreeText>
<References>
<Ref type="ON">
<Reference>PO5101903</Reference>
</Ref>
</References>
<YourReference />
<Rounding>0.0</Rounding>
<CollectionDate>2016-04-07</CollectionDate>
<CollectionTime>00:00:00</CollectionTime>
<LoadIndicator>2</LoadIndicator>
<TransportPriority>3</TransportPriority>
<TransportRefNo />
<TransportPmtIndicator />
<TransportMethodCode>1</TransportMethodCode>
<Charges>
<Charge type="FREIGHT">
<Amount>0.00</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</Charge>
</Charges>
<Allowances>
<Allowance type="Standard">
<Amount>0.00</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</Allowance>
</Allowances>
<ExciseDuties>
<ExciseDuty type="9">
<Amount>0.0</Amount>
<TaxCategory>S</TaxCategory>
<TaxAmount>0.0</TaxAmount>
<Percent>0.0</Percent>
</ExciseDuty>
</ExciseDuties>
<InvoiceDiscounts>
<InvoiceDiscount DiscountType="3">
<DiscountPeriodUnit>Day</DiscountPeriodUnit>
<DiscountPeriod />
<DiscountPercent />
</InvoiceDiscount>
</InvoiceDiscounts>
<Parties>
<Party type="Billto">
<No>6564654656546</No>
</Party>
<Party type="Sellto">
<No>6564654656546</No>
<Name>Denmark Danmark A/S</Name>
<Name2 />
<Postbox />
<Address>Anelystparken 16</Address>
<Address2 />
<BuildingNumber />
<City>Mundelstrup</City>
<PostCode>8381</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo />
<Contact>Ikke angivet</Contact>
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
<Party type="Supplier">
<No>11111111</No>
<Name>asd asd A/S</Name>
<Name2 />
<Postbox />
<Address>Gejlhavegård 17</Address>
<Address2 />
<BuildingNumber />
<City>Kolding</City>
<PostCode>6000</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo>
DK11111111
</VATRegNo>
<Contact />
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
<Party type="Shipto">
<No>19555305</No>
<Name />
<Name2 />
<Postbox />
<Address>
Denmark Hjørring
Frederikshavnsvej 290
</Address>
<Address2 />
<BuildingNumber />
<City>Hjørring</City>
<PostCode>9800</PostCode>
<CountryRegionCode>DK</CountryRegionCode>
<County />
<ContactRole />
<VATRegNo />
<Contact />
<ContactPhoneNo />
<ContactFaxNo />
<ContactEmail />
<SupplierNoByCustomer />
</Party>
</Parties>
<TaxSubtotals>
<TaxSubtotal type="S">
<TaxableAmount>7805.00</TaxableAmount>
<TaxAmount>1951.25</TaxAmount>
<TaxCategory>
<ID>S</ID>
<Percent>25.00</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxSubtotals>
<Lines>
<Line>
<LineNo>1</LineNo>
<LineRefNo>1</LineRefNo>
<EANNo>0200100218620</EANNo>
<ItemNo>OL 0308 B</ItemNo>
<RequestNotification>3</RequestNotification>
<SublineReference>1</SublineReference>
<RequestedDeliveryDate>2016-04-07</RequestedDeliveryDate>
<PromisedDeliveryDate>2016-04-07</PromisedDeliveryDate>
<PlannedDeliveryDate>2016-04-07</PlannedDeliveryDate>
<PlannedShipmentDate>2016-04-07</PlannedShipmentDate>
<ShipmentDate>2016-04-07</ShipmentDate>
<EarliestDeliveryDate>2016-04-07</EarliestDeliveryDate>
<LatestDeliveryDate>2016-04-07</LatestDeliveryDate>
<ExpiryDate>2016-04-07</ExpiryDate>
<Description>Stålvask 400 x 340 mm</Description>
<GrossWeight>0</GrossWeight>
<NetWeight>0</NetWeight>
<PacketType>Box</PacketType>
<UnitsperParcel>0</UnitsperParcel>
<UnitVolume>0</UnitVolume>
<UnitCost>0</UnitCost>
<Quantity>5.00</Quantity>
<OutstandingQuantity>0</OutstandingQuantity>
<QuantityShipped>0</QuantityShipped>
<MinimumQuantity>0</MinimumQuantity>
<CreditedQuantity>0</CreditedQuantity>
<QuantityInvoiced>5.00</QuantityInvoiced>
<UnitofMeasure>PCE</UnitofMeasure>
<UnitPrice>198.00</UnitPrice>
<NetUnitPrice>198.00</NetUnitPrice>
<Amount>990.00</Amount>
<AmountIncludingVAT>1237.50</AmountIncludingVAT>
<LineExtensionAmount>990.00</LineExtensionAmount>
<InvDiscAmounttoInvoice>0</InvDiscAmounttoInvoice>
<RetailPrice>0</RetailPrice>
<TaxSubtotals>
<TaxSubtotal type="S">
<TaxableAmount>990.00</TaxableAmount>
<TaxAmount>247.50</TaxAmount>
<TaxCategory>
<ID>S</ID>
<Percent>25.00</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxSubtotals>
<LineDiscountPct>0</LineDiscountPct>
<LineDiscountAmount>0.00</LineDiscountAmount>
</Line>
</Lines>
</Body>
</Document>
任何帮助将不胜感激
由于
答案 0 :(得分:1)
如果要更新XML中的SenderEnpointID
,您确实需要一个与该元素匹配的模板。但是,您还需要考虑名称空间。
这是因为在输入XML中,SenderEnpointID
元素位于默认命名空间http://rep.evenex.dk/schema/evenex/eBusiness_01
中。虽然您还在XSLT中定义了一个默认命名空间,但这不适用于匹配属性中的xpath表达式。
要处理此问题,您必须使用前缀再次声明命名空间,并在xpath表达式中使用它。
xmlns:e="http://rep.evenex.dk/schema/evenex/eBusiness_01"
因此,模板看起来像这样
<xsl:template match="e:SenderEndpointID">
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
<xsl:text>11111111</xsl:text>
</xsl:copy>
</xsl:template>
或者,如果您只是想更新SenderEndpointID
的文字,则可以替换与之匹配的当前模板,而不是更简单:
<xsl:template match="e:SenderEndpointID/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
您可以为Party
下的元素设置类似的模板。例如
<xsl:template match="e:Party[@type='Supplier']/e:No">
<No>
<xsl:text>11111111</xsl:text>
</No>
</xsl:template>
或许只是这个
<xsl:template match="e:Party[@type='Supplier']/e:No/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
试试这个......
<xsl:stylesheet version="1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://rep.evenex.dk/schema/evenex/eBusiness_01"
xmlns:e="http://rep.evenex.dk/schema/evenex/eBusiness_01">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="e:SenderEndpointID/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
<xsl:template match="e:Party[@type='Supplier']/e:No/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
<xsl:template match="e:Party[@type='Supplier']/e:VATRegNo/text()">
<xsl:text>DK11111111</xsl:text>
</xsl:template>
<!--Identity template, provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
请注意,如果您能够使用XSLT 2.0,则可以使用xpath-default-namespace
来设置xpath表达式的默认命名空间。