none
xpath to array element in xml RRS feed

  • Question

  • Hi,

    i have an xml which consists array of element, 

    <?xml version="1.0"?>
    -<ValidateBatchOrderByIdResponse xmlns="urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05">-<ValidateBatchOrderByIdResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="urn:absg.com:Asd:Ordering:Shopping:Type:2009:05">

    <a:ErrorEmailTypes xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">

    <b:string>BatchInvalidItem</b:string>

    <b:string>BatchDuplicateOrder</b:string>

    </a:ErrorEmailTypes>

    ...

    I want to read this String element from this xml, i am using below xpath string

    xPathStr = "/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays'][1]/text()";

    stringValue= xpath(msgInput.part, xPathStr );

    but it is not returning anything, it is returning empty string. can you please help how do get the String vaule from a:ErrorEmailTypes?

    Regards,

    Amit


    Regards, Amit More

    Thursday, September 27, 2012 9:08 AM

Answers

  • Amit use the string function as shown below:

    stringValue= xpath(msgInput.part, “string(/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays'][1]/text())");


    Please mark the post answered your question as answer, and mark other helpful posts as helpful, it'll help other users who are visiting your thread for the similar problem, Regards -Rohit Sharma (http://rohitt-sharma.blogspot.com/)

    • Marked as answer by Amit C More Thursday, September 27, 2012 10:15 AM
    Thursday, September 27, 2012 9:35 AM
    Moderator
  • hey Rohit, thanks buddy my problem is solved,

    i used below workaround,

    thanks a lot. you saved my lots of time.

    stringValue= xpath(msgInput.part, string(/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays']["+strIndex+"]/text())");

    Regards,

    amit


    Regards, Amit More

    • Marked as answer by Amit C More Thursday, September 27, 2012 10:15 AM
    Thursday, September 27, 2012 10:15 AM

All replies

  • Amit use the string function as shown below:

    stringValue= xpath(msgInput.part, “string(/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays'][1]/text())");


    Please mark the post answered your question as answer, and mark other helpful posts as helpful, it'll help other users who are visiting your thread for the similar problem, Regards -Rohit Sharma (http://rohitt-sharma.blogspot.com/)

    • Marked as answer by Amit C More Thursday, September 27, 2012 10:15 AM
    Thursday, September 27, 2012 9:35 AM
    Moderator
  • thanks Rohit for response.

    yes that's work. but it will work for one element, i am using loop for that string element and i am setting 

    strVar = "/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays']";
    xpathString = strvar + "[{0}]/text()";
    //xpathString = strvar + "[{0}]";
    xpathQuery = System.String.Format(xpathString, responseResultsIndex);

    stringValue= xpath(msgInput.part, xpathQuery);

    now if i set something like this it is not working stringValue= xpath(msgInput.part, "string(xpathQuery)"

    any idea if i want to loop through it.

    Regards,

    Amit


    Regards, Amit More

    Thursday, September 27, 2012 9:58 AM
  • hey Rohit, thanks buddy my problem is solved,

    i used below workaround,

    thanks a lot. you saved my lots of time.

    stringValue= xpath(msgInput.part, string(/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays']["+strIndex+"]/text())");

    Regards,

    amit


    Regards, Amit More

    • Marked as answer by Amit C More Thursday, September 27, 2012 10:15 AM
    Thursday, September 27, 2012 10:15 AM
  • Amit try to change it as mentioned below:

    xPathStr = "string(/*[local-name()='ValidateBatchOrderByIdResponse' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']
    		/*[local-name()='ValidateBatchOrderByIdResult' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Interface:2009:05']
    		/*[local-name()='ErrorEmailTypes' and namespace-uri()='urn:absg.com:Asd:Ordering:Shopping:Type:2009:05']
    		/*[local-name()='string' and namespace-uri()='http://schemas.microsoft.com/2003/10/Serialization/Arrays'][{0}]/text())";
    
    xpathQuery = System.String.Format(xPathStr, responseResultsIndex);
    stringValue= xpath(msgInput.part, xpathQuery);


    Please mark the post answered your question as answer, and mark other helpful posts as helpful, it'll help other users who are visiting your thread for the similar problem, Regards -Rohit Sharma (http://rohitt-sharma.blogspot.com/)

    Thursday, September 27, 2012 10:16 AM
    Moderator
  • thanks Rohit that's also worked fine.

    Regards, Amit More

    Thursday, September 27, 2012 11:44 AM