Tuesday, April 14, 2015

Index or paramter passed to XSL not working while using as xpath predicate

Some time we want to pass the index of a node as a parameter to XSL. Inside the xsl we want to use that particular index to get the right node from a complex xml document.

There are many blogs already explaining how we can achieve this.

But there is a small catch in that approach. Here I am trying to explain how we can over come that issue.

As described in other blogs, the steps to pass parameters are as shown below.

1) Defining a parameter.xsd.

Parameter.xsd
----------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://schemas.oracle.com/parameters"
            targetNamespace="http://schemas.oracle.com/parameters"
            elementFormDefault="qualified">
 <xsd:element name="parameters">
    <xsd:complexType>
      <xsd:sequence>    
              <xsd:element name="startIndex" type="xsd:string"/>
              <xsd:element name="endIndex" type="xsd:string"/>
            </xsd:sequence>  
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2) Define a bpel variable of type parameter

 <variable name="parameters" element="ns3:parameters"/>

Note: Ensure you import the target names space in our BPEL file
 xmlns:ns3="http://schemas.oracle.com/parameters"

3) Initialize the values in the above variable
<assign name="Init">
        <copy>
                <from expression="number(3)"/>
                <to variable="parameters"
                    query="/ns3:parameters/ns3:startIndex"/>
            </copy>
 </assign>

4) Pass the  variable to xsl transform function.

 <from expression="ora:doXSLTransformForDoc('xsl/xForm_CustomerOrder_To_FulfillmentOrder.xsl', $inputVariable.payload, 'parameters', $parameters)"/>

5) Inside the xsl file extract the parameters and use it in the xpath expression to pick the right node.

 <xsl:variable name="startIndex" select="$parameters/ns1:parameters/ns1:startIndex"/>
 <xsl:variable name="endIndex" select="$parameters/ns1:parameters/ns1:endIndex"/>

 Inside the xsl file  if you are using this parameter value as positional search. e.g  CustomerOrder/OrderLine[$startindex]  it will not work


To make it working we need to type cast the above  values to number while assigning to the xsl variable

 <xsl:variable name="startIndex" select="number($parameters/ns1:parameters/ns1:startIndex)"/>
 <xsl:variable name="endIndex" select="number($parameters/ns1:parameters/ns1:endIndex)"/>

Then it will work for positional search. If it is a text based search then this type casting is not required.

No comments:

Post a Comment