首页 > 解决方案 > XSLT 1.0 如果条件

问题描述

你能帮我吗?

如果“PackingslipId”-“LineInformation”-“Lines”有不同,我不喜欢在“HeaderInformation”上有“PackingslipId”和“DeliveryDate”。

如果“LineInformation”-“Item”上的“PackingslipId”始终与“HeaderInformation”上的相同,则输出“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>

最好的问候朱利安

标签: xmlxsltxslt-1.0

解决方案


问题是它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>

推荐阅读