Sammy
Sammy

Reputation: 121

Covert an XML which contains < > in it, using XSLT

I am trying to convert an XML message which is having < > in it and I am able to do it. But after conversion the "datatypes" are not getting populated.

My request XML:

<?xml version="1.0"?>
<params>
  <param dataType="java.lang.String">respID</param>
  <param dataType="java.lang.Date">2015-11-04</param>
  <param dataType="java.lang.String">

     &lt;XX_IL_OLM_COMP_ELEMENT_OBJ&gt;&lt;P_OLM_COMP_ELEMENT&gt;
&lt;XX_IL_OLM_COMP_ELEMENT_OBJ&gt;
    &lt;P_ACTION&gt;CREATE&lt;/P_ACTION&gt;
    &lt;p_activity_version_id&gt;12&lt;/p_activity_version_id&gt;
    &lt;p_learning_path_id&gt;12&lt;/p_learning_path_id&gt;
    &lt;p_certification_id&gt;1006&lt;/p_certification_id&gt;
    &lt;p_offering_id&gt;3&lt;/p_offering_id&gt;
    &lt;p_competence_id&gt;43&lt;/p_competence_id&gt;
    &lt;p_proficiency_level_id&gt;34&lt;/p_proficiency_level_id&gt;
    &lt;p_competence_element_id&gt;64&lt;/p_competence_element_id&gt;
    &lt;p_object_version_number&gt;09&lt;/p_object_version_number&gt;
    &lt;P_ERROR_CODE&gt;43&lt;/P_ERROR_CODE&gt;
    &lt;P_ERROR_MESSAGE/&gt;
&lt;/XX_IL_OLM_COMP_ELEMENT_OBJ&gt;
&lt;/P_OLM_COMP_ELEMENT&gt;
&lt;/XX_IL_OLM_COMP_ELEMENT_OBJ&gt;

  </param>
  <param dataType="java.lang.String">respID</param>
</params>

My XSLT:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

Output after transformation:

<?xml version="1.0" encoding="UTF-8"?>
<params>
  <param>respID</param>
  <param>2015-11-04</param>
  <param>

     <XX_IL_OLM_COMP_ELEMENT_OBJ><P_OLM_COMP_ELEMENT>
<XX_IL_OLM_COMP_ELEMENT_OBJ>
    <P_ACTION>CREATE</P_ACTION>
    <p_activity_version_id>12</p_activity_version_id>
    <p_learning_path_id>12</p_learning_path_id>
    <p_certification_id>1006</p_certification_id>
    <p_offering_id>3</p_offering_id>
    <p_competence_id>43</p_competence_id>
    <p_proficiency_level_id>34</p_proficiency_level_id>
    <p_competence_element_id>64</p_competence_element_id>
    <p_object_version_number>09</p_object_version_number>
    <P_ERROR_CODE>43</P_ERROR_CODE>
    <P_ERROR_MESSAGE/>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</P_OLM_COMP_ELEMENT>
</XX_IL_OLM_COMP_ELEMENT_OBJ>

  </param>
  <param>respID</param>
</params>

Desired output should have the datatypes in it as an attribute.

<?xml version="1.0" encoding="UTF-8"?>
<params>
  <param dataType="java.lang.String">respID</param>
  <param dataType="java.lang.Date">2015-11-04</param>
  <param dataType="java.lang.String">

     <XX_IL_OLM_COMP_ELEMENT_OBJ><P_OLM_COMP_ELEMENT>
<XX_IL_OLM_COMP_ELEMENT_OBJ>
    <P_ACTION>CREATE</P_ACTION>
    <p_activity_version_id>12</p_activity_version_id>
    <p_learning_path_id>12</p_learning_path_id>
    <p_certification_id>1006</p_certification_id>
    <p_offering_id>3</p_offering_id>
    <p_competence_id>43</p_competence_id>
    <p_proficiency_level_id>34</p_proficiency_level_id>
    <p_competence_element_id>64</p_competence_element_id>
    <p_object_version_number>09</p_object_version_number>
    <P_ERROR_CODE>43</P_ERROR_CODE>
    <P_ERROR_MESSAGE/>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</P_OLM_COMP_ELEMENT>
</XX_IL_OLM_COMP_ELEMENT_OBJ>

  </param>
  <param dataType="java.lang.String">respID</param>
</params>  

Can anyone help me to rectify my mistake here. Thanks in advance. Cheers!!

Upvotes: 2

Views: 1196

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

You're not processing the attributes in your param template. Either add <xsl:copy-of select="@*"/> or <xsl:apply-templates select="@*"/> to the template...

<xsl:template match="param">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>

I'd use xsl:apply-templates. That way if you need to change those attributes in the future, you just need to add a template. If you use xsl:copy-of, you'll have to add a template and change xsl:copy-of to xsl:apply-templates anyway.

Upvotes: 5

Related Questions