juls_pro_37
juls_pro_37

Reputation: 79

Otherwise XSLT1.0 - correct output

I´ve a problem at my xslt.

Here it is:

<?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:choose>
        <xsl:when test="./QUALIFIER='CN'">
		  <CONTACT_NUMBER>
		   <xsl:value-of select="CONTACT/NUMBER"/>
		  </CONTACT_NUMBER>
        </xsl:when>   
          <xsl:otherwise>          
		<xsl:if test="./QUALIFIER='ST'">
		  <CONTACT_NUMBER>
		   <xsl:value-of select="CONTACT/NUMBER"/>
		  </CONTACT_NUMBER>
        </xsl:if>
         </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>      
  	  <!--copy all other nodes-->
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  </xsl:stylesheet>

Can you tell me, whats my mistake at my xml?

I just like one of them... If Contact at CN - use this, otherwise use from ST. -> If contact at CN and ST use CN.

Edit: xml:

<SEEDELFOR>
    <GRP>           
    		<ADD>
				<QUALIFIER>CN</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
				<COUNTRY_CODE></COUNTRY_CODE>
				<CONTACT>
					<QUALIFIER></QUALIFIER>
					<NUMBER>1111111</NUMBER>
				</CONTACT>			
			</ADD>			
			<ADD>
				<QUALIFIER>ST</QUALIFIER>
				<IDENTIFIER></IDENTIFIER>
				<AGENCY_CODE></AGENCY_CODE>
				<PARTY_NAME_1></PARTY_NAME_1>
				<STREET_1></STREET_1>
				<CITY></CITY>
				<POSTAL_CODE></POSTAL_CODE>
				<CONTACT>
					<QUALIFIER></QUALIFIER>
					<NUMBER>2222222</NUMBER>
				</CONTACT>	
			</ADD>
    </GRP>
</SEEDELFOR>

How can i correct my xslt?

I hope you can help me.

Best regards Julian

Upvotes: 0

Views: 68

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117043

Consider the following simplified example:

<xsl:template match="GRP">
    <xsl:copy>
        <!-- other code, if necessary-->
        <xsl:variable name="cn" select="ADD[QUALIFIER='CN']" />
        <xsl:variable name="st" select="ADD[QUALIFIER='ST']" />
        <CONTACT_NUMBER>
            <xsl:choose>
                <xsl:when test="$cn">
                    <xsl:value-of select="$cn/CONTACT/NUMBER"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$st/CONTACT/NUMBER"/>
                </xsl:otherwise>
            </xsl:choose>
        </CONTACT_NUMBER>
        <!-- more code, if necessary-->
    </xsl:copy>
</xsl:template>

Upvotes: 1

Vebbie
Vebbie

Reputation: 1695

One of the way you can achieve this is, rewriting the template GRP as following:

<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:variable name="contact_ST">
            <xsl:for-each select="./ADD">
                <xsl:if test="./QUALIFIER='ST'">
                    <CONTACT_NUMBER>
                        <xsl:value-of select="CONTACT/NUMBER" />
                    </CONTACT_NUMBER>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <xsl:variable name="contact_CN">
            <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>
        </xsl:variable>
        <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>
        <xsl:choose>
            <xsl:when test="$contact_CN != ''">
                <xsl:copy-of select="$contact_CN" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$contact_ST" />
            </xsl:otherwise>
        </xsl:choose>

        <!--copy all other nodes -->
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

Upvotes: 1

Related Questions