0

XML:

<Report xmlns="https://defaultnamespace/abc">     
<Conversion>
<Type>FirstElement</Type>
</Conversion>
<Conversion>
<Type>SecondElement</Type>
</Conversion>
<Conversion>
<Type>ThirdElement</Type>
</Conversion>
</Report>

wso2 code:

<inSequence>
<iterate xmlns:cs="https://defaultnamespace/abc" continueparent="true" expression="//cs:Report/cs:Conversion" attachPath="//cs:Report">
<target>
<sequence>
<xslt ket="SampleXSLT.xslt"/>
</sequence>
</target>
</iterate>
</inSequence>

The issue is the above iterate mediator works only if the input XML as above works only if it contains namespace "https://defaultnamespace/abc".

Requirement:

The iterate mediator does not work if the above XML has different namespace. So, I need alternative solutions so that the iterate mediator takes namespace that comes from XML dynamically and the transformation happens. Help me with the best solution with minor modification in the code.

New Requirement:

I have a similar requirement in XSLT also.

XML:

<Report xmlns="https://defaultnamespace/abc">     
<Conversion>
<Type>FirstElement</Type>
</Conversion>
<Conversion>
<Type>SecondElement</Type>
</Conversion>
<Conversion>
<Type>ThirdElement</Type>
</Conversion>
<Last>This is last element</Last>
</Report>

XSLT:

<?xml version=1.0 encoding=UTF-8?>
<xsl:stylesheet xmlns:cs="https://defaultnamespace/abc">
<xsl:template match="/">
<xsl:for-each select="/cs:Report/cs:Conversion/cs:Type">
<xsl:element name="Converting"><xsl:value-of select="."/></xsl:element>
</xsl:for-each>
<xsl:element name="LastOne"><xsl:value-of select="/cs:Report/cs:Last"/>               </xsl:element>
</xsl:template>
</xsl:stylesheet>

The above XSLT fails when the input XML has a different namespace. So, I need an XSLT that will dynamically get the namespace from input XML and does the transformation.

Community
  • 1
  • 1

2 Answers2

1
 <iterate preservePayload="true"
          attachPath="//*[local-name()='Report']"
          expression="//*[local-name()='Report']/*[local-name()='Conversion']">
    <target>
       <sequence>
          <xslt ket="SampleXSLT.xslt"/>
       </sequence>
    </target>
 </iterate>

if you want to use actual namespace from the message :

<property name="ns" expression="namespace-uri($body/*)"/>
<iterate preservePayload="true"
         attachPath="//*[namespace-uri()=$ctx:ns and local-name()='Report']"
         expression="//*[namespace-uri()=$ctx:ns and local-name()='Report']/*[namespace-uri()=$ctx:ns and local-name()='Conversion']">
Jean-Michel
  • 5,926
  • 1
  • 13
  • 19
  • Hi, Michel thanks a lot for the answer. I believe the above solution will ignore whatever namespace that comes with the 'Report' element. Is there any alternative like we retrieve the namespace of the Report element of the incoming XML and use this namespace dynamically in the iterate mediator for the xpath expressions. For example, retrieving the namespace into a Property Mediator first and then using this property mediator that contains namespace in the iterate mediator. Thanks in advance. – masetti mani kanta Jul 02 '15 at 15:43
  • Thanks Michel. I will definitely try the logic you provided. Anyways I have a similar requirement in XSLT also. XML: ------ – masetti mani kanta Jul 02 '15 at 17:43
  • I have reposted the requirement. Please suggest the solution. – masetti mani kanta Jul 02 '15 at 18:15
0

How to retrieve namespaces in XML files using Xpath

I think the second answer in the above post shall answer your question..

Community
  • 1
  • 1
rohan
  • 166
  • 1
  • 11