Welcome to WebmasterWorld Guest from 54.162.151.77

Forum Moderators: open

Message Too Old, No Replies

XSLT 1.0 - do not specify duplicates and conditions

     
10:00 am on Oct 30, 2018 (gmt 0)

New User

joined:Oct 30, 2018
posts: 1
votes: 0


Hi, I'm new here and hope that somebody can help me. :)

With the help of another forum I managed to remove duplicates in an XML using XSLT, unfortunately I can't get any further here.

Background: In my XML, duplicates are often output under the HeaderInformation node in HeaderText.
We have managed to solve this, but currently the HeaderText node MUST always be present under HeaderInformation. - which is not always the case...

Would now like to add an extension (when?, if?, otherwise?,...) that this XSLT solution is only executed if the HeaderText node is actually present in the XML, otherwise everything should remain unchanged.

I hope that you can help me here please.

XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

<xsl:key name="header_text" match="HeaderText" use="Text"/>
<xsl:key name="line_text" match="LineText" use="Text"/>

<!-- Identity-Template für die nicht explizit benannten Elemente -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="HeaderInformation">
<HeaderInformation>
<xsl:copy-of select="HeaderText[1]/preceding-sibling::*"/>
<xsl:for-each select="HeaderText[generate-id() =
generate-id(key('header_text', Text)[1])]">
<xsl:variable name="akt_key" select="Text"/>
<xsl:variable name="akt_group" select="key('header_text', $akt_key)"/>
<xsl:for-each select="$akt_group">
<xsl:if test="position() = 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<xsl:copy-of select="HeaderText[last()]/following-sibling::*[following-sibling::LineItem]"/>
<xsl:apply-templates select="LineItem"/>
<xsl:copy-of select="LineItem/following-sibling::*"/>
</HeaderInformation>
</xsl:template>

</xsl:stylesheet>


XML with HeaderText
<?xml version="1.0" encoding="utf-8"?><SALESINVOICE>
<Interchange>
<Recipient></Recipient>
<Sender></Sender>
<CreationDate></CreationDate>
<Test></Test>
<Interchange_Control_Number></Interchange_Control_Number>
<HeaderInformation>
<HeaderText>
<Qualifier>AAI</Qualifier>
<Text>AAI10854</Text>
</HeaderText><HeaderText>
<Qualifier>DEL</Qualifier>
<Text>DEL0854</Text>
</HeaderText>
<HeaderText>
<Qualifier>AAI</Qualifier>
<Text>AAI10854</Text>
</HeaderText><HeaderText>
<Qualifier>DEL</Qualifier>
<Text>DEL0854</Text>
</HeaderText>
<OrigInvoiceNumber></OrigInvoiceNumber>
<InvoiceType></InvoiceType>
<InvoiceDate></InvoiceDate>
<InvoiceNumber></InvoiceNumber>
<CustomerOrderReference></CustomerOrderReference>
<SalesOrderReference></SalesOrderReference>
<SalesOrderDate></SalesOrderDate>
<PackingslipId></PackingslipId>
<DeliveryDate></DeliveryDate>
<Currency></Currency>
<TaxExempt></TaxExempt>
<Contact>
<Contact></Contact>
</Contact>
<TermsOfPayment>
<PaymTermDescription></PaymTermDescription>
<DueDate></DueDate>
</TermsOfPayment>
<CashDiscountConditions>
</CashDiscountConditions>
<WeightAndVolume>
<NetWeight></NetWeight>
<UnifOfWeight></UnifOfWeight>
</WeightAndVolume>
<SupplierAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
</SupplierAddressInformation>
<BuyerAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</BuyerAddressInformation>
<InvoiceAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</InvoiceAddressInformation>
<DeliveryAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<State></State>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</DeliveryAddressInformation>
<TransportDetails>
<DeliveryTerms></DeliveryTerms>
</TransportDetails>
<LineItem>
<Lines>
<OrderLineNum>1</OrderLineNum>
<GTIN></GTIN>
<GTINDescription></GTINDescription>
<SupplierArticleNumber></SupplierArticleNumber>
<Quantity></Quantity>
<PriceUnit></PriceUnit>
<FixedPrice></FixedPrice>
<SalesPrice></SalesPrice>
<DiscAmount></DiscAmount>
<DiscPercent></DiscPercent>
<SalesLinePercent1></SalesLinePercent1>
<SalesLinePercent2></SalesLinePercent2>
<MultiLnDisc></MultiLnDisc>
<MultiLnPercent></MultiLnPercent>
<LineAmount></LineAmount>
<SumLineDisc></SumLineDisc>
<DeliveredQuantity></DeliveredQuantity>
<VatBaseAmount></VatBaseAmount>
<VatPercentage></VatPercentage>
<Measure_Unit></Measure_Unit>
<LineTextt>
<Qualifier>LIN</Qualifier>
<Text>2534</Text>
</LineTextt>
</Lines>
<Lines>
<OrderLineNum>2</OrderLineNum>
<GTIN></GTIN>
<GTINDescription></GTINDescription>
<SupplierArticleNumber></SupplierArticleNumber>
<Quantity></Quantity>
<PriceUnit></PriceUnit>
<FixedPrice></FixedPrice>
<SalesPrice></SalesPrice>
<DiscAmount></DiscAmount>
<DiscPercent></DiscPercent>
<SalesLinePercent1></SalesLinePercent1>
<SalesLinePercent2></SalesLinePercent2>
<MultiLnDisc></MultiLnDisc>
<MultiLnPercent></MultiLnPercent>
<LineAmount></LineAmount>
<SumLineDisc></SumLineDisc>
<DeliveredQuantity></DeliveredQuantity>
<VatBaseAmount></VatBaseAmount>
<VatPercentage></VatPercentage>
<Measure_Unit></Measure_Unit>
<LineTextt>
<Qualifier>LIN</Qualifier>
<Text>234</Text>
</LineTextt>
</Lines>
</LineItem>
<Totals>
<InvoiceTotal></InvoiceTotal>
<NetAmount></NetAmount>
<VATAmount></VATAmount>
<DiscountAmount></DiscountAmount>
<AllowanceOrChargeAmount></AllowanceOrChargeAmount>
<DueDate></DueDate>
<VAT>
<VATS>
<Percentage></Percentage>
<Type></Type>
<BaseAmount></BaseAmount>
<VATAmount></VATAmount>
</VATS>
</VAT>
</Totals>
</HeaderInformation>
</Interchange>
</SALESINVOICE>


XML without HeaderText
<?xml version="1.0" encoding="utf-8"?><SALESINVOICE>
<Interchange>
<Recipient></Recipient>
<Sender></Sender>
<CreationDate></CreationDate>
<Test></Test>
<Interchange_Control_Number></Interchange_Control_Number>
<HeaderInformation>
<OrigInvoiceNumber></OrigInvoiceNumber>
<InvoiceType></InvoiceType>
<InvoiceDate></InvoiceDate>
<InvoiceNumber></InvoiceNumber>
<CustomerOrderReference></CustomerOrderReference>
<SalesOrderReference></SalesOrderReference>
<SalesOrderDate></SalesOrderDate>
<PackingslipId></PackingslipId>
<DeliveryDate></DeliveryDate>
<Currency></Currency>
<TaxExempt></TaxExempt>
<Contact>
<Contact></Contact>
</Contact>
<TermsOfPayment>
<PaymTermDescription></PaymTermDescription>
<DueDate></DueDate>
</TermsOfPayment>
<CashDiscountConditions>
</CashDiscountConditions>
<WeightAndVolume>
<NetWeight></NetWeight>
<UnifOfWeight></UnifOfWeight>
</WeightAndVolume>
<SupplierAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
</SupplierAddressInformation>
<BuyerAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</BuyerAddressInformation>
<InvoiceAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</InvoiceAddressInformation>
<DeliveryAddressInformation>
<GLN></GLN>
<Name></Name>
<Street></Street>
<ZipCode></ZipCode>
<City></City>
<Country></Country>
<State></State>
<VATNum></VATNum>
<InternalNumber></InternalNumber>
</DeliveryAddressInformation>
<TransportDetails>
<DeliveryTerms></DeliveryTerms>
</TransportDetails>
<LineItem>
<Lines>
<OrderLineNum>1</OrderLineNum>
<GTIN></GTIN>
<GTINDescription></GTINDescription>
<SupplierArticleNumber></SupplierArticleNumber>
<Quantity></Quantity>
<PriceUnit></PriceUnit>
<FixedPrice></FixedPrice>
<SalesPrice></SalesPrice>
<DiscAmount></DiscAmount>
<DiscPercent></DiscPercent>
<SalesLinePercent1></SalesLinePercent1>
<SalesLinePercent2></SalesLinePercent2>
<MultiLnDisc></MultiLnDisc>
<MultiLnPercent></MultiLnPercent>
<LineAmount></LineAmount>
<SumLineDisc></SumLineDisc>
<DeliveredQuantity></DeliveredQuantity>
<VatBaseAmount></VatBaseAmount>
<VatPercentage></VatPercentage>
<Measure_Unit></Measure_Unit>
</Lines>
<Lines>
<OrderLineNum>2</OrderLineNum>
<GTIN></GTIN>
<GTINDescription></GTINDescription>
<SupplierArticleNumber></SupplierArticleNumber>
<Quantity></Quantity>
<PriceUnit></PriceUnit>
<FixedPrice></FixedPrice>
<SalesPrice></SalesPrice>
<DiscAmount></DiscAmount>
<DiscPercent></DiscPercent>
<SalesLinePercent1></SalesLinePercent1>
<SalesLinePercent2></SalesLinePercent2>
<MultiLnDisc></MultiLnDisc>
<MultiLnPercent></MultiLnPercent>
<LineAmount></LineAmount>
<SumLineDisc></SumLineDisc>
<DeliveredQuantity></DeliveredQuantity>
<VatBaseAmount></VatBaseAmount>
<VatPercentage></VatPercentage>
<Measure_Unit></Measure_Unit>
</Lines>
</LineItem>
<Totals>
<InvoiceTotal></InvoiceTotal>
<NetAmount></NetAmount>
<VATAmount></VATAmount>
<DiscountAmount></DiscountAmount>
<AllowanceOrChargeAmount></AllowanceOrChargeAmount>
<DueDate></DueDate>
<VAT>
<VATS>
<Percentage></Percentage>
<Type></Type>
<BaseAmount></BaseAmount>
<VATAmount></VATAmount>
</VATS>
</VAT>
</Totals>
</HeaderInformation>
</Interchange>
</SALESINVOICE>


THANKS

LG Julian