I am using xsl to transform xml to xml. Could you please help me to write xsl code to convert input to output? I need the data as rich text data in CDATA for first two tags. Thanks in advance.
Input:
<ATTRIBUTE-VALUE>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">
<h1 dir="ltr" id="_1536217498885">Main Description</h1>
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
<p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166?accept=none&private"/>**</p>
<h1 dir="ltr" id="_1536217498886">Key Consideration</h1>
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
<h1 dir="ltr" id="_1536217498887">Skills</h1>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
<h1 dir="ltr" id="_1536217498888">Synonyms</h1>
<p>The Synonyms text goes here.</p>
</div>
</THE-VALUE>
</ATTRIBUTE-VALUE>
Output:
<MainDescription>
<![CDATA[
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
<p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg"/>**</p>
]]>
</MainDescription>
<KeyConsiderations>
<![CDATA[
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
]]>
</KeyConsiderations>
<Skills>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
</Skills>
<Synonyms>
<p>The Synonyms text goes here.</p>
</Synonyms>
I am able to get elements from h1 using below code. But I don't have clue for getting values of '< p >' so I marked it as ?????????. Please help to get solution for ?????????.
<xsl:for-each select="my:THE-VALUE/xhtml:div/xhtml:h1">
<xsl:variable name="ReqIFTextTags" select="translate(., ' ', '')"></xsl:variable>
<xsl:element name="{$ReqIFTextTags}">
<xsl:value-of select="?????????"></xsl:value-of>
</xsl:element>
</xsl:for-each>
Wrapping the siblings following the
h1
elements into a wrapper element created from theh1
element in XSLT 1 is possible with a key:Online at https://xsltfiddle.liberty-development.net/bdxtqy.
Serializing the contents of those elements to markup is best done with an extension function (if your used XSLT 1 processor has one or easily allows setting it up) or with a library for that task like http://lenzconsulting.com/xml-to-string/xml-to-string.xsl, then you can serialize the elements:
Having a CDATA section requires to know the elements in advance and name then in advance e.g.
<xsl:output cdata-section-elements="MainDescription KeyConsideration"/>
, as I have done in above sample, also online at https://xsltfiddle.liberty-development.net/bdxtqy/1.As you have the original elements in the XHTML namespace but your desired output has the serialized
p
elements in no namespace, you would first need to push the elements through a template that strips the namespace and then push them through the modexml-to-string
, this additionally requires the use of an extension function likeexsl:node-set
:https://xsltfiddle.liberty-development.net/bdxtqy/2