1

I have an xml with repeating DPART segment and need to pick ADDRESS value from any segment having PARTN_ROLE as 'UU' and use this address to get variables of NAME and STREET from other repeatable segment DCOAD. so need to extract 2 variables:-

<xsl:variable name="Cust_Name"
<xsl:variable name="Cust_Street"

.

Output variable should be like:-

<Cust_Name>Michael</Cust_Name>
<Cust_Street>ABCH</Cust_Street>

Part of Input XML:-

<Z1E1P SEGMENT="1">
    <ORDER>5467899</ORDER>
    <ACCOUNT>X</ACCOUNT>
    <Z1BP_ISAORDER SEGMENT="1">         
       <DPART SEGMENT="1">
        <PARTN_ROLE>JK</PARTN_ROLE>
        <CONTACT>0000000000</CONTACT>
        <ADDRESS>0000027647</ADDRESS>
       </DPART>
       <DPART SEGMENT="1">
        <PARTN_ROLE>UU</PARTN_ROLE>
        <CONTACT>0000000000</CONTACT>
        <ADDRESS>9164412232</ADDRESS>
       </DPART>
       <DCOAD SEGMENT="1">
        <ADDRESS>0000023378</ADDRESS>
        <NAME>John</NAME>
        <STREET>gyhu</STREET>
        <COUNTRY>US</COUNTRY>
       </DCOAD>
       <DCOAD SEGMENT="1">
        <ADDRESS>9164412232</ADDRESS>
        <NAME>Michael</NAME>
        <STREET>ABCH</STREET>
        <COUNTRY>US</COUNTRY>
       </DCOAD>
    </Z1BP_ISAORDER>
</Z1E1P>

It needs to be handled using XSLT 1.0 I tried below XSLT but not getting the output, part of xslt:-

<xsl:variable name="Cust_Name">
    <xsl:if test="node()/Z1BP_ISAORDER/DPART[PARTN_ROLE='UU']/ADDRESS = node()/Z1BP_ISAORDER/DCOAD/ADDRESS">
        <xsl:copy-of select="node()/Z1BP_ISAORDER/DCOAD/NAME">
        </xsl:copy-of>
    </xsl:if>
</xsl:variable>

<xsl:variable name="Cust_Street">
    <xsl:if test="node()/Z1BP_ISAORDER/DPART[PARTN_ROLE='UU']/ADDRESS = node()/Z1BP_ISAORDER/DCOAD/ADDRESS">
        <xsl:copy-of select="node()/Z1BP_ISAORDER/DCOAD/STREET">
        </xsl:copy-of>
    </xsl:if>
</xsl:variable>

2 Answers 2

1

It is not clear in what context you declare the variables. But I suppose this will give you a good start:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:variable name="allSegments"  select="/Z1E1P/Z1BP_ISAORDER/*"/>
  
  <xsl:variable name="addressNumber"  select="$allSegments[PARTN_ROLE='UU']/ADDRESS/text()"/>
  <xsl:variable name="segmentToUse"   select="$allSegments[not(PARTN_ROLE='UU') and ADDRESS/text()=$addressNumber]"/>
      
  <xsl:variable name="Cust_Name"      select="$segmentToUse/NAME"/>
  <xsl:variable name="Cust_Street"    select="$segmentToUse/STREET"/>
  
  <xsl:template match="/">
    <xsl:copy-of select="$Cust_Name"/>
    <xsl:copy-of select="$Cust_Street"/>
  </xsl:template>

</xsl:stylesheet>

Will output:

<NAME>Michael</NAME>
<STREET>ABCH</STREET>
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand your question correctly (which is not at all certain), you want to lookup the customer name and store based on matching ADDRESS value. This is best done by defining a key and using it. Here is an example:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="cust-by-addr" match="DCOAD" use="ADDRESS" />

<xsl:template match="Z1E1P">
    <output>
        <xsl:for-each select="Z1BP_ISAORDER/DPART[PARTN_ROLE='UU']">
            <xsl:variable name="addr" select="key('cust-by-addr', ADDRESS)" />
            <Customer>
                <Cust_Name>
                    <xsl:value-of select="$addr/NAME" />
                </Cust_Name>
                <Cust_Street>
                    <xsl:value-of select="$addr/STREET" />
                </Cust_Street>
            </Customer>
        </xsl:for-each>
    </output>   
</xsl:template>

</xsl:stylesheet>

Applied to your input example, this will return:

<?xml version="1.0" encoding="UTF-8"?>
<output>
  <Customer>
    <Cust_Name>Michael</Cust_Name>
    <Cust_Street>ABCH</Cust_Street>
  </Customer>
</output>

Not sure what variables have to do with this. You cannot output a variable. You can only use it for temporary storage. Here I used a variable only to avoid repeating the same lookup twice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.