XSLT 1.0,如果每个

时间:2018-11-08 12:18:02

标签: xml xslt xslt-1.0

你能帮我吗?

我想从我的XML中获得两个信息。

  1. 如果预选赛CN(添加)将CONTACT / NUMBER添加到GRP
  2. 如果预选赛资格赛(附加)将PARTY_NAME_1,...添加到GRP

我尝试了以下XSLT :(但请在下面的答案中找到新的XSLT,如果正确的话?)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
	
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template> 
  
   <xsl:template match="GRP">
    <xsl:copy>
	<!--copy the data from ADD - CN to the GRP so it can be used in the mapping to set the contact from end customer-->
      <xsl:for-each  select ="./ADD">
		<xsl:if test="./QUALIFIER='CN'">          
		  <CONTACT_NUMBER>
		   <xsl:value-of select="CONTACT/NUMBER"/>
		  </CONTACT_NUMBER>
        </xsl:if>
      </xsl:for-each>
  	  <!--copy all other nodes-->
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
     <xsl:template match="GRP">
    <xsl:copy>
	<!--copy the data from ADD - ST to the GRP so it can be used in the mapping to set the delivery address from end customer-->
      <xsl:for-each  select ="./ADD">
		<xsl:if test="./QUALIFIER='ST'">
          <xsl:copy-of select="PARTY_NAME_1"/>    
          <xsl:copy-of select="STREET_1"/>
		  <xsl:copy-of select="CITY"/>
		  <xsl:copy-of select="POSTAL_CODE"/>
		  <xsl:copy-of select="COUNTRY_CODE"/>		  
        </xsl:if>
      </xsl:for-each>
  	  <!--copy all other nodes-->
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!--delete IC node-->
  <xsl:template match="IC" />
  
  <!--delete FILTER_SET node-->
  <xsl:template match="FILTER_SET" />
 
  <!--delete SEE_AG node-->
  <xsl:template match="SEE_AG" />

  <!--delete ME node-->
  <xsl:template match="ME" />

  <!--delete REF node-->
  <xsl:template match="REF" />
  
  <!--delete TRANSPORT_DETAILS node-->
  <xsl:template match="TRANSPORT_DETAILS" />
  
  <!--delete PACKAGE_DETAILS node-->
  <xsl:template match="PACKAGE_DETAILS" />
  
  <!--delete AMOUNT_DETAILS node-->
  <xsl:template match="AMOUNT_DETAILS" />
  
   <!-- 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"?>
<SEEDELFOR>
	<SEE_AG COPYRIGHT="" CREATION_DATE="2007-05-09" MESSAGE="" MAPPING="" VERSION="1.1.54"/>
	<CNT>
		<TRANSMISSION_DATE>20181024</TRANSMISSION_DATE>
		<TRANSMISSION_TIME>060300</TRANSMISSION_TIME>
		<INTERCHANGE_CONTROL_NUMBER>1240</INTERCHANGE_CONTROL_NUMBER>
		<SENDER></SENDER>
		<SENDER_QUALIFIER>14</SENDER_QUALIFIER>
		<RECEIVER></RECEIVER>
		<RECEIVER_QUALIFIER>14</RECEIVER_QUALIFIER>
		<SYNTAX_IDENTIFIER>UNOC</SYNTAX_IDENTIFIER>
		<SYNTAX_VERSION>3</SYNTAX_VERSION>
		<GRP>			
			<IDENTIFIER_BY></IDENTIFIER_BY>
			<IDENTIFIER_MF></IDENTIFIER_MF>
			<IDENTIFIER_ST></IDENTIFIER_ST>
			<IDENTIFIER_SU></IDENTIFIER_SU>
			<IDENTIFIER_UD></IDENTIFIER_UD>
			<IDENTIFIER_CA></IDENTIFIER_CA>
			<DATE_4></DATE_4>
			<REF_ON></REF_ON>
			<TXT_001>
				<QUALIFIER></QUALIFIER>
				<TEXT_LINE></TEXT_LINE>
			</TXT_001>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER>CN</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1>Name</PARTY_NAME_1>
				<STREET_1>Street</STREET_1>
				<CITY>City</CITY>
				<POSTAL_CODE>12345</POSTAL_CODE>
				<COUNTRY_CODE>DE</COUNTRY_CODE>
				<CONTACT>
					<QUALIFIER>TE</QUALIFIER>
					<NUMBER>012345/6789_CN</NUMBER>
				</CONTACT>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER>ST</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1>Name_ST</PARTY_NAME_1>
				<STREET_1>Street_ST</STREET_1>
				<CITY>City_ST</CITY>
				<POSTAL_CODE>12345_ST</POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
			</ADD>
			<TRANSPORT_DETAILS>
				<ADDITIONAL_DETAILS>
					<QUALIFIER></QUALIFIER>
					<DETAILS></DETAILS>
				</ADDITIONAL_DETAILS>
			</TRANSPORT_DETAILS>
			<ITEM>
				<ITEM_NUMBER_EN></ITEM_NUMBER_EN>
				<ITEM_NUMBER_IN></ITEM_NUMBER_IN>
				<ITEM_NUMBER_SA></ITEM_NUMBER_SA>
				<QUANTITY></QUANTITY>
				<QUANTITY_UNIT></QUANTITY_UNIT>
				<LINE_ITEM_NUMBER></LINE_ITEM_NUMBER>
				<AMOUNT_DETAILS>
					<QUALIFIER></QUALIFIER>
					<AMOUNT></AMOUNT>
					<CURRENCY></CURRENCY>
				</AMOUNT_DETAILS>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<PACKAGE_DETAILS>
					<NUMBER_OF_PACKAGES></NUMBER_OF_PACKAGES>
					<REF>
						<QUALIFIER></QUALIFIER>
						<REFERENCE></REFERENCE>
					</REF>
					<REF>
						<QUALIFIER></QUALIFIER>
						<REFERENCE></REFERENCE>
					</REF>
				</PACKAGE_DETAILS>
			</ITEM>
		</GRP>
	</CNT>
</SEEDELFOR>

正确的输出应为:

<?xml version="1.0"?>
<SEEDELFOR>
	<SEE_AG COPYRIGHT="" CREATION_DATE="2007-05-09" MESSAGE="" MAPPING="" VERSION="1.1.54"/>
	<CNT>
		<TRANSMISSION_DATE>20181024</TRANSMISSION_DATE>
		<TRANSMISSION_TIME>060300</TRANSMISSION_TIME>
		<INTERCHANGE_CONTROL_NUMBER>1240</INTERCHANGE_CONTROL_NUMBER>
		<SENDER></SENDER>
		<SENDER_QUALIFIER>14</SENDER_QUALIFIER>
		<RECEIVER></RECEIVER>
		<RECEIVER_QUALIFIER>14</RECEIVER_QUALIFIER>
		<SYNTAX_IDENTIFIER>UNOC</SYNTAX_IDENTIFIER>
		<SYNTAX_VERSION>3</SYNTAX_VERSION>
		<GRP>
			<CONTACT_NUMBER>012345/6789_CN</CONTACT_NUMBER>
			<PARTY_NAME_1>Name_ST</PARTY_NAME_1>
			<STREET_1>Street_ST</STREET_1>
			<CITY>City_ST</CITY>
			<POSTAL_CODE>12345_ST</POSTAL_CODE>
			<IDENTIFIER_BY></IDENTIFIER_BY>
			<IDENTIFIER_MF></IDENTIFIER_MF>
			<IDENTIFIER_ST></IDENTIFIER_ST>
			<IDENTIFIER_SU></IDENTIFIER_SU>
			<IDENTIFIER_UD></IDENTIFIER_UD>
			<IDENTIFIER_CA></IDENTIFIER_CA>
			<DATE_4></DATE_4>
			<REF_ON></REF_ON>
			<TXT_001>
				<QUALIFIER></QUALIFIER>
				<TEXT_LINE></TEXT_LINE>
			</TXT_001>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER>CN</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1>Name</PARTY_NAME_1>
				<STREET_1>Street</STREET_1>
				<CITY>City</CITY>
				<POSTAL_CODE>12345</POSTAL_CODE>
				<COUNTRY_CODE>DE</COUNTRY_CODE>
				<CONTACT>
					<QUALIFIER>TE</QUALIFIER>
					<NUMBER>012345/6789_CN</NUMBER>
				</CONTACT>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER>ST</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1>Name_ST</PARTY_NAME_1>
				<STREET_1>Street_ST</STREET_1>
				<CITY>City_ST</CITY>
				<POSTAL_CODE>12345_ST</POSTAL_CODE>
			</ADD>
			<ADD>
				<QUALIFIER></QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
			</ADD>
			<TRANSPORT_DETAILS>
				<ADDITIONAL_DETAILS>
					<QUALIFIER></QUALIFIER>
					<DETAILS></DETAILS>
				</ADDITIONAL_DETAILS>
			</TRANSPORT_DETAILS>
			<ITEM>
				<ITEM_NUMBER_EN></ITEM_NUMBER_EN>
				<ITEM_NUMBER_IN></ITEM_NUMBER_IN>
				<ITEM_NUMBER_SA></ITEM_NUMBER_SA>
				<QUANTITY></QUANTITY>
				<QUANTITY_UNIT></QUANTITY_UNIT>
				<LINE_ITEM_NUMBER></LINE_ITEM_NUMBER>
				<AMOUNT_DETAILS>
					<QUALIFIER></QUALIFIER>
					<AMOUNT></AMOUNT>
					<CURRENCY></CURRENCY>
				</AMOUNT_DETAILS>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<REF>
					<QUALIFIER></QUALIFIER>
					<REFERENCE></REFERENCE>
					<ADDITIONAL_INFO/>
				</REF>
				<PACKAGE_DETAILS>
					<NUMBER_OF_PACKAGES></NUMBER_OF_PACKAGES>
					<REF>
						<QUALIFIER></QUALIFIER>
						<REFERENCE></REFERENCE>
					</REF>
					<REF>
						<QUALIFIER></QUALIFIER>
						<REFERENCE></REFERENCE>
					</REF>
				</PACKAGE_DETAILS>
			</ITEM>
		</GRP>
	</CNT>
</SEEDELFOR>

谢谢,最好的问候 朱利安

1 个答案:

答案 0 :(得分:0)

这正确吗?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
	
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template> 
  
      <xsl:template match="GRP">
    <xsl:copy>
	<!--copy the data from ADD - ST to the GRP so it can be used in the mapping to set the delivery address from end customer-->
      <xsl:for-each  select ="./ADD">
		<xsl:if test="./QUALIFIER='ST'">
          <xsl:copy-of select="PARTY_NAME_1"/>    
          <xsl:copy-of select="STREET_1"/>
		  <xsl:copy-of select="CITY"/>
		  <xsl:copy-of select="POSTAL_CODE"/>
		  <xsl:copy-of select="COUNTRY_CODE"/>		  
        </xsl:if>
		<xsl:if test="./QUALIFIER='CN'">
		  <CONTACT_NUMBER>
		   <xsl:value-of select="CONTACT/NUMBER"/>
		  </CONTACT_NUMBER>
        </xsl:if>
      </xsl:for-each>
  	  <!--copy all other nodes-->
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!--delete IC node-->
  <xsl:template match="IC" />
  
  <!--delete FILTER_SET node-->
  <xsl:template match="FILTER_SET" />
 
  <!--delete SEE_AG node-->
  <xsl:template match="SEE_AG" />

  <!--delete ME node-->
  <xsl:template match="ME" />

  <!--delete REF node-->
  <xsl:template match="REF" />
  
  <!--delete TRANSPORT_DETAILS node-->
  <xsl:template match="TRANSPORT_DETAILS" />
  
  <!--delete PACKAGE_DETAILS node-->
  <xsl:template match="PACKAGE_DETAILS" />
  
  <!--delete AMOUNT_DETAILS node-->
  <xsl:template match="AMOUNT_DETAILS" />
  
   <!-- 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>