My Source XSD for XSL transformation:
<?xml version= '1.0' encoding= 'UTF-8' ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.quo.com"
targetNamespace="http://www.quo.com"
elementFormDefault="qualified"
xmlns:emc="http://www.quo.com"
<xsd:complexType name="HeaderType">
<xsd:sequence>
<xsd:element name="messageId" type="xsd:string"/>
<xsd:element name="messageType" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="version" type="xsd:int"/>
</xsd:complexType>
<xsd:complexType name="ResponseType">
<xsd:sequence>
<xsd:element name="errorCode" type="xsd:string"/>
<xsd:element name="errorString" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="AckMessage">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="header" type="HeaderType"/>
<xsd:element name="payload" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="response" type="ResponseType"/>
</xsd:schema>
My Sample XML for Source XSD is:
<AckMessage>
<header version="1">
<messageId>messageId1</messageId>
<messageType>sourceSystemId2</messageType>
</header>
<payload>
<bookstore>
<book>
<title lang="en">Harry Potter</title>
<author>J K Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book>
<title lang="en">Harry Potter 1</title>
<author>J K Rowling</author>
<year>2006</year>
<price>29.99</price>
</book>
</bookstore>
</payload>
</AckMessage>
The payload element in source XSD is string type in which I am getting the whole XML fragment for root element bookstore
My Target XSD for XSL transformation:
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.quo.com/ns/bookstore"
targetNamespace="http://www.quo.com/ns/bookstore"
elementFormDefault="qualified">
<xsd:element name="bookstore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="lang" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="year" type="xsd:integer"/>
<xsd:element name="price" type="xsd:float"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I want to transform the Payload element from source to bookstore element of target schema.
When I trying to do by simple transformation, I am getting error that cannot transform simple element to XML element.
I tried xsl:
<xsl:variable name="variable1">
<xsl:value-of select="/msg_in_out:esbAckMessage/msg_in_out:payload"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
</xsl:variable>
<xsl:template match="/">
<imp1:bookstore>
<xsl:value-of select="$variable1"/>
</imp1:bookstore>
</xsl:template>
</xsl:stylesheet>
But I am getting output as
<?xml version = '1.0' encoding = 'UTF-8'?>
<imp1:bookstore xmlns:imp1="http://www.quo.com/ns/bookstore">
Harry Potter
J K Rowling
2005
29.99
Harry Potter 1
J K Rowling
2006
29.99
</imp1:bookstore>
How can I get everything inside Payload element with Xml tags?
The answer is use copy-of
<xsl:variable name="variable1">
<xsl:value-of select="/msg_in_out:esbAckMessage/msg_in_out:payload"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
</xsl:variable>
<xsl:template match="/">
<imp1:bookstore>
<xsl:copy-of select="$variable1"/>
</imp1:bookstore>
</xsl:template>
</xsl:stylesheet>
payload
element is of type string then the alleged input sample you have shown does not make sense at all, it either needs escaped markup or a CDATA section. – Martin Honnen