XSLT 1.0-不指定重复项和条件

时间:2018-11-02 06:53:57

标签: xml xslt xslt-1.0

在另一个论坛的帮助下,我设法使用XSLT删除了XML中的重复项,不幸的是,我在这里再也找不到了。

背景:在我的XML中,通常在HeaderText的HeaderInformation节点下输出重复项。 我们已经设法解决了这个问题,但是当前HeaderText节点必须始终存在于HeaderInformation下。 -并非总是如此...

我想添加一个扩展名(何时?,是否?,否则?,...),以便仅在XML中确实存在HeaderText节点时才执行此XSLT解决方案,否则,所有内容都应保持不变。

我的带有HeaderText的XML

<?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>

我没有HeaderText的XML

<?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>

我的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>

“行”->“ LineText”处出现相同的问题。

希望您能在这里为我提供帮助。

谢谢

朱利安(LG Julian)

1 个答案:

答案 0 :(得分:0)

我认为您需要做的就是修改模板匹配项,以检查是否存在HeaderText节点

 <xsl:template match="HeaderInformation[HeaderText]">

如果HeaderInformation没有HeaderText,则身份模板将与之匹配,因此所有内容均保持不变。

请注意,我对这三行感到困惑

  <xsl:copy-of select="HeaderText[last()]/following-sibling::*[following-sibling::LineItem]"/>
  <xsl:apply-templates select="LineItem"/>
  <xsl:copy-of select="LineItem/following-sibling::*"/>

您似乎正在尝试选择HeaderText之后但LineItem之前的所有内容。然后选择LineItem,然后选择所有内容。但这实际上与执行此操作相同:

 <xsl:apply-templates select="HeaderText[last()]/following-sibling::*"/>

当您拥有身份模板后,此处进行xsl:apply-templates与进行xsl:copy-of相同。 (我猜对于LineItem,您实际上有一个与之匹配的模板,因此它将优先于身份模板)。

此外,您的<xsl:for-each select="HeaderText[generate-id() = generate-id(key('header_text', Text)[1])]">语句(包括其主体)实际上可以简化为...

<xsl:copy-of select="HeaderText[generate-id() = generate-id(key('header_text', Text)[1])]" />

尝试使用此XSLT

<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[HeaderText]">
    <HeaderInformation>
      <xsl:apply-templates select="HeaderText[1]/preceding-sibling::*"/>
      <xsl:copy-of select="HeaderText[generate-id() = generate-id(key('header_text', Text)[1])]" />
      <xsl:apply-templates select="HeaderText[last()]/following-sibling::*"/>
    </HeaderInformation>
  </xsl:template>
</xsl:stylesheet>

话虽如此,您可以采用一种甚至更简单的方法,那就是与其显式地复制不同的元素,不如考虑删除重复的元素。

也尝试使用此XSLT

<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="HeaderText[generate-id() != generate-id(key('header_text', Text)[1])]" />
</xsl:stylesheet>

您在这里也会为LineText使用类似的模板。