Reputation: 56
I want to create XSLT for copying same input XML but want to change few XML tags with static value.
I had tried my self building XSLT, but i can see output XML is same as input XML.
Here is the Input XML file
<?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>
I want to generate same output file but few tags updated with static value.
Here is the xslt code that i tried to make,
<?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>
I want to update SenderEndpointID in Header tag with fixed value '11111111'
Also <No>
and <VATRegNo>
tag inside <Party type="Supplier">
- rest of the tags will remain same.
<Party type="Supplier">
<No>11111111</No>
<VATRegNo>DK11111111</VATRegNo>
</Party>
So final output should look like
<?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>
Any help would be appreciated
Thanks
Upvotes: 0
Views: 645
Reputation: 70598
If you want to update the SenderEnpointID
in your XML, you really need a template that matches that element. However, you also need to take into account namespace.
This is because in the input XML the SenderEnpointID
element is in a default namespace http://rep.evenex.dk/schema/evenex/eBusiness_01
. Although you have also defined a default namespace in your XSLT, this won't apply to xpath expressions in match attributes.
To handle this, you will have to declare the namespace again with a prefix, and use that in your xpath expression.
xmlns:e="http://rep.evenex.dk/schema/evenex/eBusiness_01"
So, the template will look like this
<xsl:template match="e:SenderEndpointID">
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
<xsl:text>11111111</xsl:text>
</xsl:copy>
</xsl:template>
Alternatively, if you just wanted to update the text of SenderEndpointID
, you can replace the current template matching it, with this simpler one instead:
<xsl:template match="e:SenderEndpointID/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
You can have similar templates for the elements under Party
. For example
<xsl:template match="e:Party[@type='Supplier']/e:No">
<No>
<xsl:text>11111111</xsl:text>
</No>
</xsl:template>
Or maybe just this
<xsl:template match="e:Party[@type='Supplier']/e:No/text()">
<xsl:text>11111111</xsl:text>
</xsl:template>
Try this...
<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>
Note that if you were able to use XSLT 2.0, you could make use of the xpath-default-namespace
which will set a default namespace for xpath expressions.
Upvotes: 1