This discussion is archived
4 Replies Latest reply: Aug 15, 2011 6:39 AM by odie_63 RSS

sort and extract specific elements where attribute name equals to some val

882261 Newbie
Currently Being Moderated
Hi,

Need some help I have a below structure and need to extract all the elements where attribute name=test1
<params>
  <param name="test51" value="val3353" />
  <param name="test1" value="val2" />
  <param name="test3" value="val3" />
  <param name="test4" value="val4" />
  <param name="test1" value="val1" />
  <param name="test1" value="val11" />
  <param name="test8" value="val33" />
  <param name="test1" value="val34" />
  <param name="test8" value="val33" />
  <param name="test81" value="val33" />
  <param name="test1" value="val333" />
  <param name="test38" value="val33" />
</params>
Basically need the below output after extracting name=test1 from the above.
<mparams>
  <param name="test1" value="val2" />
  <param name="test1" value="val1" />
  <param name="test1" value="val11" />
  <param name="test1" value="val34" />
  <param name="test1" value="val333" />
</mparams>
Thanks
  • 1. Re: sort and extract specific elements where attribute name equals to some val
    odie_63 Guru
    Currently Being Moderated
    Hi,

    Since you posted in the XQuery forum, I assume you're looking for an XQuery expression to do the job (if not please post additional details).

    This one should do it :
    <mparams>
    {
     $doc/params/param[@name="test1"]
    }
    </mparams>
    where $doc is a variable containing the input XML document.
  • 2. Re: sort and extract specific elements where attribute name equals to some val
    882261 Newbie
    Currently Being Moderated
    Hi,

    I tried that but somehow it's not working. Here is the input to my xquery transformation
    <soapenv:Body  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
       <InputParams> 
         <queryParams> 
           <quer:params  xmlns:quer="http://www.example.org/QueryParams"> 
            <param name="test51" value="val3353" />
            <param name="test1" value="val2" />
            <param name="test3" value="val3" />
            <param name="test4" value="val4" />
            <param name="test1" value="val1" />
            <param name="test1" value="val11" />
            <param name="test8" value="val33" />
            <param name="test1" value="val34" />
            <param name="test8" value="val33" />
            <param name="test81" value="val33" />
            <param name="test1" value="val333" />
            <param name="test38" value="val33" />
           </quer:query-params> 
         </Params> 
       </InputParams> 
    </soapenv:Body> 
    and want to have the below output after running the below XQuery transformation
    <ns0:mparams>
      <ns0:param name="test1" value="val2" />
      <ns0:param name="test1" value="val1" />
      <ns0:param name="test1" value="val11" />
      <ns0:param name="test1" value="val34" />
      <ns0:param name="test1" value="val333" />
    </ns0:mparams>
    this is the xquery transformation file
    (:: pragma  parameter="$InputQuery" type="anyType" ::)
    (:: pragma bea:global-element-return element="ns0:mparams" location="../xsd/TargetParams.xsd" ::)
    
    declare namespace ns0 = "http://www.example.org/QueryParams";
    declare namespace xf = "http://tempuri.org/Simple/transformation/listparams/";
    
    declare function xf:listparams($InputQuery as element(*))
        as element(ns0:mparams) {
            <ns0:mparams>
                { $InputQuery/ns0:params/param[@name="test1"] }
            </ns0:mparams>
    };
    
    declare variable $InputQuery as element(*) external;
    
    xf:listparams($InputQuery)
    but the output is always empty and here is what it comes out
    <quer:mparams xmlns:quer="http://www.example.org/QueryParams"/>
    Thanks for any help
  • 3. Re: sort and extract specific elements where attribute name equals to some val
    tsuji Journeyer
    Currently Being Moderated
    The function should read at least this, amid all the imperfection.
    declare function xf:listparams($InputQuery as element(*))
        as element(ns0:mparams) {
            <ns0:mparams>
            {
                  for $p in $InputQuery/ns0:params/param[@name="test1"]
                  return <param name="{$p/@name}" value="{$p/@value}" />
            }
            </ns0:mparams>
    };
  • 4. Re: sort and extract specific elements where attribute name equals to some val
    odie_63 Guru
    Currently Being Moderated
    Here is the input to my xquery transformation
    Your input is not well formed.
    Should we read this instead?
    <soapenv:Body  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
       <InputParams> 
         <queryParams> 
           <quer:params  xmlns:quer="http://www.example.org/QueryParams"> 
            <param name="test51" value="val3353" />
            <param name="test1" value="val2" />
            <param name="test3" value="val3" />
            <param name="test4" value="val4" />
            <param name="test1" value="val1" />
            <param name="test1" value="val11" />
            <param name="test8" value="val33" />
            <param name="test1" value="val34" />
            <param name="test8" value="val33" />
            <param name="test81" value="val33" />
            <param name="test1" value="val333" />
            <param name="test38" value="val33" />
           </quer:params> 
         </queryParams>
       </InputParams> 
    </soapenv:Body>
    Anyway, you have to include a few more steps in the XPath expression, and declare the soapenv namespace too.
    So, elaborating on Tsuji's suggestion :
    declare namespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
    declare namespace ns0 = "http://www.example.org/QueryParams";
    declare namespace xf = "http://tempuri.org/Simple/transformation/listparams/";
     
    declare function xf:listparams($InputQuery as element(*))
    as element(ns0:mparams) 
    {
      <ns0:mparams>
      { 
        for $i in $InputQuery/soap:Body/InputParams/queryParams/ns0:params/param[@name="test1"]
        return element ns0:param {$i/@name, $i/@value} 
      }
      </ns0:mparams>
    };
     
    declare variable $InputQuery as element(*) external;
     
    xf:listparams($InputQuery)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points