XSLT 1.0,如果条件

时间:2019-01-23 09:02:26

标签: xml xslt xslt-1.0

你能帮我吗?

如果“ PackingslipId”-“ LineInformation”-“ Lines”处存在差异,我不希望“ HeaderInformation”上具有“ PackingslipId”和“ DeliveryDate”。

如果“ LineingInformation”-“ Item”上的“ PackingslipId”始终与“ HeaderInformation”上的“ PackingslipId”相同,则输出“ PackingslipId”和“ DeliveryDate”就可以。

请注意:(我不是很重要,也可能是,只有一篇文章(“ LineInformation”-“ Item”-“ OrderLineNum”为1)-在这种情况下为“ PackingslipId”与“ Item”和“ HeaderInformation”相同,因此“ PackingslipId”和“ DeliveryDate”也应位于“ HeaderInformation”和“ LineItem”上

我的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SALESINVOICE>
	<Interchange>
		<Recipient></Recipient>
		<Sender></Sender>
		<CreationDate></CreationDate>
		<Test></Test>
		<Interchange_Control_Number></Interchange_Control_Number>
	</Interchange>
	<HeaderInformation>
		<OrigInvoiceNumber>1</OrigInvoiceNumber>
		<InvoiceType>INVOIC</InvoiceType>
		<InvoiceDate>2019.01.23</InvoiceDate>
		<InvoiceNumber></InvoiceNumber>
		<PurchOrderReference>fddf</PurchOrderReference>
		<SalesOrderReference></SalesOrderReference>
		<CustomerOrderReference>fdgfdg</CustomerOrderReference>
		<PackingslipId>1079304</PackingslipId>
		<DeliveryDate>2019.01.23</DeliveryDate>
		<Currency></Currency>
		<WeightAndVolume />
		<DeliveryAddressInformation>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
		</DeliveryAddressInformation>
		<InvoiceAddressInformation>
			<GLN></GLN>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
			<VATNum></VATNum>
			<InternalNumber></InternalNumber>
		</InvoiceAddressInformation>
		<BuyerAddressInformation />
		<SupplierAddressInformation>
			<GLN></GLN>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
			<VATNum></VATNum>
			<Contact />
		</SupplierAddressInformation>
	</HeaderInformation>
	<LineInformation>
		<Item>
			<OrderLineNum>1</OrderLineNum>
			<GTIN></GTIN>
			<GTINDescription></GTINDescription>
			<PackingslipId>1079304</PackingslipId>
			<DeliveryDate>2019.01.23</DeliveryDate>
			<SupplierArticleNumber></SupplierArticleNumber>
			<Quantity>1</Quantity>
			<DeliveredQuantity>1</DeliveredQuantity>
		</Item>
	</LineInformation>
	<LineInformation>
		<Item>
			<OrderLineNum>2</OrderLineNum>
			<GTIN></GTIN>
			<GTINDescription></GTINDescription>
			<PackingslipId>1079305</PackingslipId>
			<DeliveryDate>2019.01.23</DeliveryDate>
			<SupplierArticleNumber></SupplierArticleNumber>
			<Quantity>1</Quantity>
			<DeliveredQuantity>1</DeliveredQuantity>
		</Item>
	</LineInformation>
	<Totals />
</SALESINVOICE>

我的XSLT:

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

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 
  <!-- Delete PackingslipId and DeliveryDate on HeaderInformation, if PackingslipId on Line is different -->  
  <xsl:template match="HeaderInformation[PackingslipId != LineInformation/Item/PackingslipId]/*[self::PackingslipId or self::DeliveryDate]" />
    
   <xsl:template match="LineInformation_TEMP">
    <xsl:apply-templates select="node()"/>
  </xsl:template>
    
  <!-- delete empty nodes
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
 <xsl:template match="*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>
 delete empty nodes -->
 
</xsl:stylesheet>

正确的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SALESINVOICE>
	<Interchange>
		<Recipient></Recipient>
		<Sender></Sender>
		<CreationDate></CreationDate>
		<Test></Test>
		<Interchange_Control_Number></Interchange_Control_Number>
	</Interchange>
	<HeaderInformation>
		<OrigInvoiceNumber>1</OrigInvoiceNumber>
		<InvoiceType>INVOIC</InvoiceType>
		<InvoiceDate>2019.01.23</InvoiceDate>
		<InvoiceNumber></InvoiceNumber>
		<PurchOrderReference>fddf</PurchOrderReference>
		<SalesOrderReference></SalesOrderReference>
		<CustomerOrderReference>fdgfdg</CustomerOrderReference>		
		<Currency></Currency>
		<WeightAndVolume />
		<DeliveryAddressInformation>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
		</DeliveryAddressInformation>
		<InvoiceAddressInformation>
			<GLN></GLN>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
			<VATNum></VATNum>
			<InternalNumber></InternalNumber>
		</InvoiceAddressInformation>
		<BuyerAddressInformation />
		<SupplierAddressInformation>
			<GLN></GLN>
			<Name></Name>
			<Street></Street>
			<ZipCode></ZipCode>
			<City></City>
			<Country></Country>
			<VATNum></VATNum>
			<Contact />
		</SupplierAddressInformation>
	</HeaderInformation>
	<LineInformation>
		<Item>
			<OrderLineNum>1</OrderLineNum>
			<GTIN></GTIN>
			<GTINDescription></GTINDescription>
			<PackingslipId>1079304</PackingslipId>
			<DeliveryDate>2019.01.23</DeliveryDate>
			<SupplierArticleNumber></SupplierArticleNumber>
			<Quantity>1</Quantity>
			<DeliveredQuantity>1</DeliveredQuantity>
		</Item>
	</LineInformation>
	<LineInformation>
		<Item>
			<OrderLineNum>2</OrderLineNum>
			<GTIN></GTIN>
			<GTINDescription>/GTINDescription>
			<PackingslipId>1079305</PackingslipId>
			<DeliveryDate>2019.01.23</DeliveryDate>
			<SupplierArticleNumber></SupplierArticleNumber>
			<Quantity>1</Quantity>
			<DeliveredQuantity>1</DeliveredQuantity>
		</Item>
	</LineInformation>
	<Totals />
</SALESINVOICE>

最好的问候 朱利安

1 个答案:

答案 0 :(得分:1)

问题是LineInformation不是HeaderInformation的子元素,而是同级元素,因此请使用..获取父元素,然后再获取同级元素,如下所示....

<xsl:template match="HeaderInformation[PackingslipId != ../LineInformation/Item/PackingslipId]
                     /*[self::PackingslipId or self::DeliveryDate]" />

编辑:如果出于某种原因想要在比赛中不使用条件表达式的情况下进行操作,请尝试使用此XSLT ...

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

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

  <!-- Delete PackingslipId and DeliveryDate on HeaderInformation, if PackingslipId on Line is different -->  
  <xsl:template match="HeaderInformation">
    <xsl:copy>
      <xsl:choose>
        <xsl:when test="PackingslipId != ../LineInformation/Item/PackingslipId">
          <xsl:apply-templates select="node()|@*" mode="exclude" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="node()|@*" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="PackingslipId|DeliveryDate" mode="exclude" />

  <xsl:template match="node()|@*" mode="exclude">
    <xsl:copy>
     <xsl:apply-templates select="node()|@*" mode="exclude" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>