#SAPGYAN: Fetch Only Last Tag Data in xml using xslt in sap mii

Hi All,
Today we will learn about how we can access only last tag data from any given xml using XPATH and XSLT.
Given input xml: 
<?xml version="1.0" encoding="UTF-8"?>
<Rowset>
  <Row>
    <Handle>ITEMBO:SITE_NAME,TEST_MAT1,A</Handle>
  </Row>
  <Row>
    <Handle>ITEMBO:SITE_NAME,TEST_MAT2,A</Handle>
  </Row>
  <Row>
    <Handle> ITEMBO:SITE_NAME,TEST_MAT3,A </Handle>
  </Row>
</Rowset>
Now the requirement is to fetch only last Row tag data from the xml that means we need only ITEMBO:SITE_NAME,TEST_MAT3,A from the given xml.
Approach 1:
We can achieve this scenario using xslt. For this we need to design one xslt that will compare the Row tag position to the last position.
So for this we need to include following line in our xslt code.
<xsl:value-of select="//Row[position () = last()]/Handle"/>
This will compare position of Row tag in xml with the Last position defined by last() function. And will get only last Row tag data in output.
So at last our xslt will look like :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:value-of select="//Row[position() = last()]/Handle"/>
</xsl:template>
</xsl:stylesheet>
And our output will be like :
<?xml version="1.0" encoding="UTF-8"?> ITEMBO:SITE_NAME,TEST_MAT3,A
That is last row tag data coming in output using xslt.
Approach 2:
If you are using SAP MII then you can directly give this xpath in link editor to get this output.
Just write
XML_Variable_name{//Row[position()=last()]/Handle}
Where XML_Variable_name = variable (Transaction/local) that is holding the xml.
in link editor and output will be the same as xslt.
Hope you like this scenario and feel free to share your thoughts and also if you have any other approach and scenario on the same.

Ankit Gupta

Comments

Popular Posts