Monday, 29 September 2014

Extract substring using XSLT / XPath using XSLT Transformation (to use in WSO2)



I'm quite a newbie in XSLT / XPath ....


I've the following XML message ...



<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://ift.tt/18hkEkn">
<soapenv:Body>
<request xmlns="http://ift.tt/IrJ6pr">
<gam:process xmlns:gam="http://ift.tt/10fzveu">
<!--Zero or more repetitions:-->
<gam:id>1</gam:id>
<gam:id>3</gam:id>
<gam:id>5</gam:id>
<gam:id>7</gam:id>
<gam:id>438</gam:id>
<gam:id>2</gam:id>
<gam:id>4</gam:id>
<gam:id>6</gam:id>
<gam:id>8</gam:id>
</gam:process>
<ax2586:good xmlns:ax2586="http://ift.tt/10fzveu">
<ax2586:id>1</ax2586:id>
<ax2586:id>3</ax2586:id>
<ax2586:id>5</ax2586:id>
<ax2586:id>7</ax2586:id>
</ax2586:good>
</request>
</soapenv:Body>
</soapenv:Envelope>


and I'd like to obtain something like this one



<soapenv:Envelope xmlns:soapenv="http://ift.tt/18hkEkn">
<soapenv:Body>
<ns:processResponse xmlns:ns="http://ift.tt/10fzveu">
<ns:return xsi:type="ax2586:Result" xmlns:ax2586="http://ift.tt/10fzvLj" xmlns:xsi="http://ift.tt/ra1lAU">
<ax2586:good xsi:type="ax2586:ElencoId" xmlns:ax2586="http://ift.tt/10fzveu">
<ax2586:good>
<ax2586:id>1</ax2586:id>
<ax2586:id>3</ax2586:id>
<ax2586:id>5</ax2586:id>
<ax2586:id>7</ax2586:id>
</ax2586:good>
</ax2586:good>
<ax2586:message>Message Store does not exist.</ax2586:message>
<ax2586:statusCode>0</ax2586:statusCode>
<ax2586:wrong xsi:type="ax2586:ElencoId">
<ax2586:wrong xmlns:ax2586="http://ift.tt/10fzveu">
<gam:id xmlns:gam="http://ift.tt/10fzveu" xmlns="http://ift.tt/IrJ6pr">438</gam:id>
<gam:id xmlns:gam="http://ift.tt/10fzveu" xmlns="http://ift.tt/IrJ6pr">2</gam:id>
<gam:id xmlns:gam="http://ift.tt/10fzveu" xmlns="http://ift.tt/IrJ6pr">4</gam:id>
<gam:id xmlns:gam="http://ift.tt/10fzveu" xmlns="http://ift.tt/IrJ6pr">6</gam:id>
<gam:id xmlns:gam="http://ift.tt/10fzveu" xmlns="http://ift.tt/IrJ6pr">8</gam:id>
</ax2586:wrong>
</ax2586:wrong>
</ns:return>
</ns:processResponse>


Searching on the web now I've built this XSLT Transformation



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR" xmlns:xsd="http://ift.tt/tphNwY" version="2.0">
<xsl:template match="/">
<xsl:variable name="ns1" select="//*[local-name()='process']/*"/>
<xsl:variable name="ns2" select="//*[local-name()='good']/*"/>
<xsl:variable name="difference" select="$ns1[not(.=$ns2)],$ns2[not(.=$ns1)]"/>
<ax2586:wrong xmlns:ax2586="http://ift.tt/10fzveu">
<ax2586:wrong><xsl:copy-of select="$difference"/></ax2586:wrong>
</ax2586:wrong>
</xsl:template>
</xsl:stylesheet>


that seems working fine but trying to use it in different XSLT Tester on the web I've found that somewhere it works and somewhere doesn't work so I'm not so sure that this could be the right and portable solution.


NOTE: I've to use it in a WSO2 proxy


Any suggestion or alternative way for obtain the same result?


Thank you very much in advance


Cesare


No comments:

Post a Comment