Finding the XPath with the node name

Posted by julien.schneider(at)oracle.com on Oracle Blogs See other posts from Oracle Blogs or by julien.schneider(at)oracle.com
Published on Sat, 15 Jan 2011 13:44:56 +0100 Indexed on 2011/01/14 7:56 UTC
Read the original article Hit count: 446

A function that i find missing is to get the Xpath expression of a node. For example, suppose i only know the node name <theNode>, i'd like to get its complete path /Where/is/theNode.

 

Using this rather simple Xquery you can easily get the path to your node.

declare namespace orcl = "http://www.oracle.com/weblogic_soa_and_more";

declare function orcl:findXpath($path as element()*)

as xs:string

{ if(local-name($path/..)='')

then local-name($path)

else concat(orcl:findXpath($path/..),'/',local-name($path)) };

declare function orcl:PathFinder($inputRecord as element(), $path as element())

as element(*)

{

{ for $index in $inputRecord//*[local-name()=$path/text()]

return orcl:findXpath($index) }

};

declare variable $inputRecord as element() external;

declare variable $path as element() external;

orcl:PathFinder($inputRecord, $path)

 

With a path

        <myNode>nodeName</myNode>

 and a message

        <node1><node2><nodeName>test</nodeName></node2></node1>

 the result will be

        node1/node2/nodeName

 

This is particularly useful when you use the Validate action of OSB because Validate only returns the xml node which is in error and not the full location itself. The following OSB project reuses this Xquery to reformat the result of the Validate Action.

Just send an invalid xml like

<myElem http://blogs.oracle.com/weblogic_soa_and_more">http://blogs.oracle.com/weblogic_soa_and_more">
      <mySubElem>
      </mySubElem>
</myElem>

 

you'll get as nice

<MessageIsNotValid>
<ErrorDetail  nbr="1">
<dataElementhPath>Body/myElem/mySubElem</dataElementhPath>
<message>
Expected element 'Subelem1@http://blogs.oracle.com/weblogic_soa_and_more' before the end of the content in element mySubElem@http://blogs.oracle.com/weblogic_soa_and_more
</message>
</ErrorDetail>
</MessageIsNotValid>

 

Download the OSB project : sbconfig_xpath.jar

 

Enjoy.

 

 

 

 

 

 

© Oracle Blogs or respective owner

Related posts about OSB Validate XQuery Xpath