XSLT 1.0 如果条件



你能帮我吗?

如果"PackingslipId"-"

LineInformation"-"Lines"有不同之处,我不喜欢在"HeaderInformation"上有"PackingslipId"和"DeliveryDate"。

如果"PackingslipId"在"LineInformation"-"Item"上与在"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>

此致敬意朱利安

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

相关内容

  • 没有找到相关文章

最新更新