如何使用xsl来循环XML

时间:2021-06-30 14:29:56

I'm trying to transform an XML document using XSLT. I have got so far but I can't get the grouping level correct. I want each of the ASN numbers to have there own section. I've tried placing the For loops at different locations but no joy.

我正在尝试使用XSLT转换XML文档。到目前为止,我还没有得到正确的分组级别。我希望每个ASN编号都有自己的部分。我试过把For循环放在不同的位置,但是没有乐趣。

XML:-

XML:-

<?xml version="1.0" encoding="utf-8"?>
<Orders>
	<Order>
		<och_contract>FO</och_contract>
		<SourceLocation>W235X</SourceLocation>
		<SourceLocationName>ARMSTRONG FASENINGS LTD</SourceLocationName>
		<DestinationLocation>3C</DestinationLocation>
		<och_type>P</och_type>
		<CollectionDate>20170814</CollectionDate>
		<Customer_Ref>FO-323296</Customer_Ref>
		<Plan_m3>3.60</Plan_m3>
		<Plan_Kgs>2500.00</Plan_Kgs>
		<Actual_M3>0.00</Actual_M3>
		<Actual_Kgs>0.00</Actual_Kgs>
		<och_trip_ref>IBHC0141733001H</och_trip_ref>
		<ASN_Numbers>
			<ASN>STU1234</ASN>
			<ASN>STU2345</ASN>
			<ASN>STU3456</ASN>
		</ASN_Numbers>
	</Order>
	<Order>
		<och_contract>FO</och_contract>
		<SourceLocation>A07LA</SourceLocation>
		<SourceLocationName>STOKES GROUP LTD</SourceLocationName>
		<DestinationLocation>3C</DestinationLocation>
		<och_type>P</och_type>
		<CollectionDate>20170817</CollectionDate>
		<Customer_Ref>FO-323430</Customer_Ref>
		<Plan_m3>2.88</Plan_m3>
		<Plan_Kgs>2500.00</Plan_Kgs>
		<Actual_M3>5.40</Actual_M3>
		<Actual_Kgs>14782.00</Actual_Kgs>
		<och_trip_ref>IBHC0491733004H</och_trip_ref>
		<ASN_Numbers>
			<ASN>ANDY1234</ASN>
			<ASN>ANDY2345</ASN>
			<ASN>ASN3456</ASN>
		</ASN_Numbers>
	</Order>
</Orders>

XSLT:-

XSLT:-

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/apps/otm file:///C:/Users/andfishe/Documents/Projects/OTM%20Training/OTM/GLogXML_6.3.xsd">
	<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/">
<INVOICE>
			<xsl:call-template name="Invoice"/>
			<xsl:call-template name="Shipment"/>
</INVOICE>
</xsl:template>
<xsl:template name="Invoice">
<CARRIER_GSDB>V0K2Q</CARRIER_GSDB>
<INV_REF>6L027195</INV_REF>
</xsl:template>
<xsl:template name="Shipment">
<xsl:for-each select="Orders/Order">
<SHIPMENT>
<BILL_CURR>GBX</BILL_CURR>
	<STOP_GSDB>
		<xsl:value-of select="SourceLocation"/>
	</STOP_GSDB>
	
	<STOP_NAME>
		<xsl:value-of select="SourceLocationName"/>
	</STOP_NAME>
		<LINE_ITEM>
			<xsl:for-each select="ASN_Numbers">
				<ASN>
					<ASN_REF>
						<xsl:value-of select="ASN"/>
					</ASN_REF>
				</ASN>
			</xsl:for-each>
		</LINE_ITEM>
	
</SHIPMENT>
	</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Current Output:-

电流输出:

<?xml version="1.0" encoding="UTF-8"?>
<INVOICE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<CARRIER_GSDB>V0K2Q</CARRIER_GSDB>
	<INV_REF>6L027195</INV_REF>
	<SHIPMENT>
		<BILL_CURR>GBX</BILL_CURR>
		<STOP_GSDB>W235X</STOP_GSDB>
		<STOP_NAME>ARMSTRONG FASENINGS LTD</STOP_NAME>
		<LINE_ITEM>
			<ASN>
				<ASN_REF>STU1234 STU2345 STU3456</ASN_REF>
			</ASN>
		</LINE_ITEM>
	</SHIPMENT>
	<SHIPMENT>
		<BILL_CURR>GBX</BILL_CURR>
		<STOP_GSDB>A07LA</STOP_GSDB>
		<STOP_NAME>STOKES GROUP LTD</STOP_NAME>
		<LINE_ITEM>
			<ASN>
				<ASN_REF>ANDY1234 ANDY2345 ASN3456</ASN_REF>
			</ASN>
		</LINE_ITEM>
	</SHIPMENT>
</INVOICE>

I would like the LINE_ITEM section to look like:-

我希望LINE_ITEM部分看起来像:-

<?xml version="1.0" encoding="UTF-8"?>
<INVOICE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<CARRIER_GSDB>V0K2Q</CARRIER_GSDB>
	<INV_REF>6L027195</INV_REF>
	<SHIPMENT>
		<BILL_CURR>GBX</BILL_CURR>
		<STOP_GSDB>W235X</STOP_GSDB>
		<STOP_NAME>ARMSTRONG FASENINGS LTD</STOP_NAME>
		<LINE_ITEM>
			<ASN>
				<ASN_REF>STU1234</ASN_REF>
			</ASN>
			<ASN>
				<ASN_REF>STU2345</ASN_REF>
			</ASN>
			<ASN>
				<ASN_REF>STU3456</ASN_REF>
			</ASN>
		</LINE_ITEM>
	</SHIPMENT>
	<SHIPMENT>
		<BILL_CURR>GBX</BILL_CURR>
		<STOP_GSDB>A07LA</STOP_GSDB>
		<STOP_NAME>STOKES GROUP LTD</STOP_NAME>
		<LINE_ITEM>
			<ASN>
				<ASN_REF>ANDY1234</ASN_REF>
			</ASN>
			<ASN>
				<ASN_REF>ANDY2345</ASN_REF>
			</ASN>
			<ASN>
				<ASN_REF>ASN3456</ASN_REF>
			</ASN>
		</LINE_ITEM>
	</SHIPMENT>
</INVOICE>

Does anybody know how to acheive this please?

有人知道怎么做吗?

2 个解决方案

#1


3  

        <LINE_ITEM>
        <xsl:for-each select="ASN_Numbers/ASN">
            <ASN>
                <ASN_REF>                           
                    <xsl:value-of select="."/>
                </ASN_REF>
            </ASN>
        </xsl:for-each>
    </LINE_ITEM>

#2


1  

Change this part:

改变这部分:

<xsl:for-each select="ASN_Numbers">
    <ASN>
        <ASN_REF>
            <xsl:value-of select="ASN"/>
        </ASN_REF>
    </ASN>
</xsl:for-each>

to:

:

<xsl:for-each select="ASN_Numbers/ASN">
    <ASN>
        <ASN_REF>
            <xsl:value-of select="."/>
        </ASN_REF>
    </ASN>
</xsl:for-each>

#1


3  

        <LINE_ITEM>
        <xsl:for-each select="ASN_Numbers/ASN">
            <ASN>
                <ASN_REF>                           
                    <xsl:value-of select="."/>
                </ASN_REF>
            </ASN>
        </xsl:for-each>
    </LINE_ITEM>

#2


1  

Change this part:

改变这部分:

<xsl:for-each select="ASN_Numbers">
    <ASN>
        <ASN_REF>
            <xsl:value-of select="ASN"/>
        </ASN_REF>
    </ASN>
</xsl:for-each>

to:

:

<xsl:for-each select="ASN_Numbers/ASN">
    <ASN>
        <ASN_REF>
            <xsl:value-of select="."/>
        </ASN_REF>
    </ASN>
</xsl:for-each>