Saturday 31 December 2011

XPATH in OSB

In this blog I’ll show you some useful XPATH expression in OSB, hope it would reduce lot of searching time if you are new to this subject.To get started I’ll use below XSD in OSB project.

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.oracle.com"
            targetNamespace="http://www.oracle.com"
            elementFormDefault="qualified">
  <xsd:element name="PurchaseOrder">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="date" type="xsd:string"/>
        <xsd:element name="custID" type="xsd:string"/>
        <xsd:element name="items">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="item" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="description" type="xsd:string"/>
                    <xsd:element name="department" type="xsd:string"/>
                    <xsd:element name="price" type="xsd:float"/>
                    <xsd:element name="quantity" type="xsd:integer"/>
                  </xsd:sequence>
                  <xsd:attribute name="id" type="xsd:string"/>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="cardDetails">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="type" type="xsd:string"/>
              <xsd:element name="cardNumber" type="xsd:string"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="id" type="xsd:integer"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
and here is the corresponding xml generated out of that above XSD.

<?xml version="1.0" encoding="UTF-8"?>
<p:PurchaseOrder id="1234" xmlns:p="http://www.oracle.com">
  <p:date>2010-04-02</p:date>
  <p:custID>C7123843</p:custID>
  <p:items>
    <p:item id="GF234324">
      <p:description>Denim Jeans</p:description>
      <p:department>Clothing</p:department>
      <p:price>30.99</p:price>
      <p:quantity>2</p:quantity>
    </p:item>
    <p:item id="HD312782">
      <p:description>iPod 80Gb White</p:description>
      <p:department>Electrical</p:department>
      <p:price>99.99</p:price>
      <p:quantity>1</p:quantity>
    </p:item>
    <p:item id="HD998775">
      <p:description>iPod Headphones</p:description>
      <p:department>Electrical</p:department>
      <p:price>19.99</p:price>
      <p:quantity>1</p:quantity>
    </p:item>
    <p:item id="KD123984">
      <p:description>Frying Pan</p:description>
      <p:department>Home</p:department>
      <p:price>9.99</p:price>
      <p:quantity>1</p:quantity>
    </p:item>
  </p:items>
  <p:cardDetails>
    <p:type>Mastercard</p:type>
    <p:cardNumber>1234-5678-1234-5678</p:cardNumber>
  </p:cardDetails>
</p:PurchaseOrder>

So here are the required scenarios and solution where actual path might be unknown in XML tree .

1.I want to retrieve the node where item id="HD998775.

Solution: $body//*:item[@id='HD998775']

2. I want to retrieve department name of 2nd item node.

Solution: data($body/*:PurchaseOrder/*:items/*:item[2]/*:department)

3.I want to retrieve department name of 4th item node using relative path.

Solution: data($body//*:item[4]/*:department)

So like above you can write any complex XPATH expression.