cancel
Showing results for 
Search instead for 
Did you mean: 

Xml String mapped to XML Node

Former Member
0 Kudos

Hi All,

I have a structure like

<?xml version="1.0" encoding="UTF-8"?>

<ResponsePayload>

<RespString><?xml version="1.0" encoding="UTF-8"?><Devices>

<Device><Name>Nuts</Name><Units>5</Units></Device><Device><Name>Bulbs</Name><Units>6</Units></Device><Device><Name>Screws</Name><Units>3</Units></Device></Devices></RespString>

</ResponsePayload>

In the "RespString" element I have the xml string. I need to map this xml string to a node in my target mapping. Am having a XML in a single string in mah request and wanna convert that into a XML node in an xml

<?xml version="1.0" encoding="UTF-8"?>

<ResponsePayload>

<Devices>

<Device><Name>Nuts</Name><Units>5</Units></Device>

<Device><Name>Bulbs</Name><Units>6</Units></Device>

<Device><Name>Screws</Name><Units>3</Units></Device>

</Devices>

</ResponsePayload>

Thx in advance

Ravijeet

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hello Udo,

we have a simailar requirement where the source and target structures that I have created for test are belw :

Souce structure sent :

<?xml version="1.0" encoding="UTF-8"?>

<ResponsePayload>

<RespString><?xml version="1.0" encoding="UTF-8"?>

<Devices>

<Device>1</Device>

<Name>1</Name>

</Devices></RespString>

</ResponsePayload>

Target:

<?xml version="1.0" encoding="UTF-8"?>

<Devices>

<Device/>

<Name/>

</Devices>

I used the below steps as suggested by you:

XSLT 1:

<?xml version="1.0" encoding="UTF-8" ?>

- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" />

- <xsl:template match="/">

- <xsl:for-each select="//RespString">

<xsl:value-of select="." disable-output-escaping="yes" />

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

xslt 2:

<?xml version="1.0" encoding="UTF-8" ?>

- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

- <xsl:template match="/">

- <ResponsePayload>

<xsl:copy-of select="//Devices" />

</ResponsePayload>

</xsl:template>

</xsl:stylesheet>

Iam getting the below error while executing :

javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The processing instruction target matching "[xX][mM][lL]" is not allowed

Could you please let me know why this error is occuring?

Thanks

Rajesh

udo_martens
Active Contributor
0 Kudos

Hi Rajesh,

plz open a new thread for a new question.

Regards,

Udo

Former Member
0 Kudos

Hi,

I have opened a new thread .Request you to please provide if there are any inputs on the same .

Thanks

Rajesh

udo_martens
Active Contributor
0 Kudos

Hi Ravijeet,

you can reach that with two simple styles:

First one deserializes and avoid getting the XML declaration:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output omit-xml-declaration="yes"/>
	<xsl:template match="/">
		<xsl:for-each select="//RespString">
			<xsl:value-of select="." disable-output-escaping="yes"/>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Second maps the root element around...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<ResponsePayload>
			<xsl:copy-of select="//Devices"/>
		</ResponsePayload>
	</xsl:template>
</xsl:stylesheet>

Regards,

Udo

RaghuVamseedhar
Active Contributor
0 Kudos

Hi Ravijeet,

When I compare input and output, only

<RespString><?xml version="1.0" encoding="UTF-8"?>

and

</RespString>

are missing in output.

Am I right?

Regards,

Raghu_Vamsee

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>>> Am having a XML in a single string in mah request and wanna convert that into a XML node in an xml

Solution: Create java mapping to achieve this. Basically you have to read that element entire xml string and create node and all the structure elements and map it on the target side.

You have to do consecutive mapping. First use message mapping for all the elements except that xml string element. Then this java mapping look for the getElementsByTagName('xmlstring') to fetch th xml string and read the entire string xml and create nodes and subelements according to the data sent in that input xml string

Question: How will you handle if the input string xml has syntax errors?

Hope answered your question.