Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Looping through records in XML Variable !!

Avatar

Level 4

Hi,

I am getting no way how to do this. I am getting the below XML from a custom DSC component. Now I need to process it.

<root>
    <records total_records="4">
        <record>
            <jobno>1122</jobno>
            <cust_name>Abhinav</cust_name>
            <email>abhinavs@plabs.com</email>
            <stat>1</stat>
        </record>
        <record>
            <jobno>2233</jobno>
            <cust_name>Doctor Dhober</cust_name>
            <email>abhinavs@plabs.com</email>
            <stat>0</stat>
        </record>
        <record>
            <jobno>4666</jobno>
            <cust_name>Vinau Dubey</cust_name>
            <email>abhinavs@plabs.com</email>
            <stat>1</stat>
        </record>
        <record>
            <jobno>7677</jobno>
            <cust_name>Nelesh Poda</cust_name>
            <email>abhinavs@plabs.com</email>
            <stat>0</stat>
        </record>
    </records>
</root>

Now I need to loop through every record & do some stuff for each one. In my process I have to send a mail to each of the user in the XML file. I tried it using SET VALUE's Node Set Functions, but not successful. Any help.

Thanks.


-

Abhinav

1 Accepted Solution

Avatar

Correct answer by
Level 8

Your xpath doesn't match your XML - you are missing the root node in your xpath expression:

Expression: /process_data/myXML/records/record[1]/email

should be:

           /process_data/myXML/root/records/record[1]/email

When you build your loop there is one other thing to be careful of.  If you evaluate an xpath variable inside of another xpath expression, it gets inserted as a string.  This happens even if the variable is an integer.  For example:  If I have an integer variable counter which is set to 3 and evaluate:

     /process_data/myXML/root/records/record[/process_data/@counter]/email    The xpath expression will insert "3" (a string) and not an integer as you would expect.  The result will not be the third node. This can be a source of frustration and its hard to debug.

Instead, use the number function inside the expression:

  /process_data/myXML/root/records/record[number(/process_data/@counter)]/email

I've attached a sample process that shows this.

View solution in original post

4 Replies

Avatar

Level 4

I tried in set value opr:

Location: strEmail is String var in process

/process_data/@strEmail


Expression: myXML is XML var in process

/process_data/myXML/records/record[1]/@email

Result expected

strEmail = 'abhinavs@plabs.com'

But didn't getting it. Pls help.

Thanks.

-

Abhinav

Avatar

Employee

In your xml document, email is a node - not an attribute.

The xPath expression should be:

/process_data/myXML/records/record[1]/email

The location uses the @strEmail notation because as an LC process variable, it's a simple value (String) and treated as an attribute of the process.

Avatar

Level 4

Thanks Marcel for your response. I tried the way you told, but still I am not getting the node value from xml.

Location: /process_data/@strEmail 
Expression: /process_data/myXML/records/record[1]/email

strEmail is still null/blank. I checked myXML variable, it holds the proper XML as above.

I am stucked in it, help!!.

-

Abhinav

Avatar

Correct answer by
Level 8

Your xpath doesn't match your XML - you are missing the root node in your xpath expression:

Expression: /process_data/myXML/records/record[1]/email

should be:

           /process_data/myXML/root/records/record[1]/email

When you build your loop there is one other thing to be careful of.  If you evaluate an xpath variable inside of another xpath expression, it gets inserted as a string.  This happens even if the variable is an integer.  For example:  If I have an integer variable counter which is set to 3 and evaluate:

     /process_data/myXML/root/records/record[/process_data/@counter]/email    The xpath expression will insert "3" (a string) and not an integer as you would expect.  The result will not be the third node. This can be a source of frustration and its hard to debug.

Instead, use the number function inside the expression:

  /process_data/myXML/root/records/record[number(/process_data/@counter)]/email

I've attached a sample process that shows this.