4

I've got an XML export from Access 2003 and I tried to insert CDATA tag on text field (latin...) with XSLT but I'm very bad in XSLT...

Here the XML source :

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
      <export_x005F_xml_message>
       <libelle>h euismod tincidu </libelle>
       <price>300</price>
       <libelle2>h euirci tation ullamc</libelle2>
    </export_x005F_xml_message>
    <export_x005F_xml_message>
      <libelle>h euismod tincidunt ut lao</libelle>
      <price>200</price>
      <libelle2>h euirci tation ullamcorper</libelle2>
   </export_x005F_xml_message>
  </dataroot>

Here my XSLT's beginning... :

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml"/>
     <xsl:template match='*[name() = "MESSAGES"]'>
           <xsl:text disable-output-escaping="yes">
             &lt;![CDATA[
           </xsl:text>
           <xsl:copy-of select="./node()"/>
           <xsl:text disable-output-escaping="yes">
             ]]&gt;
            </xsl:text>
     </xsl:template>
    </xsl:stylesheet>

I'd like to get something like that :

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
      <export_x005F_xml_message>
       <libelle><![CDATA[h euismod tincidu ]]></libelle>
       <price>300</price>
       <libelle2><![CDATA[h euirci tation ullamc ]]></libelle>
    </export_x005F_xml_message>
    <export_x005F_xml_message>
      <libelle><![CDATA[h euismod tincidunt ut lao ]]></libelle2>
      <price>200</price>
      <libelle2><![CDATA[h euirci tation ullamcorper ]]></libelle2>
   </export_x005F_xml_message>
  </dataroot>

Can you help me to create the right XSLT ? This XML is coming from Access 2003 which doesn't provide the CDATA option for text field... I'm sure a generic model could help other developer like me :-)

Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
Jim 007
  • 310
  • 1
  • 3
  • 8

3 Answers3

3

As already answered here: Transform XML with XSLT and preserve CDATA (in Ruby), a better answer is to use xsl:output. For example ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" cdata-section-elements="libelle libelle2" />

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

</xsl:stylesheet>
Community
  • 1
  • 1
Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
2

This identity transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"
  cdata-section-elements="libelle libelle2"/>
 <xsl:strip-space elements="*"/>

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

when applied on the provided XML document (corrected as it was severely malformed):

<dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd"
 generated="2012-07-31T13:25:46">
    <export_x005F_xml_message>
        <libelle>h euismod tincidu </libelle>
        <price>300</price>
        <libelle2>h euirci tation ullamc</libelle2>
    </export_x005F_xml_message>
    <export_x005F_xml_message>
        <libelle>h euismod tincidunt ut lao</libelle>
        <price>200</price>
        <libelle2>h euirci tation ullamcorper</libelle2>
    </export_x005F_xml_message>
</dataroot>

produces the wanted, correct result:

<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
   <export_x005F_xml_message>
      <libelle><![CDATA[h euismod tincidu ]]></libelle>
      <price>300</price>
      <libelle2><![CDATA[h euirci tation ullamc]]></libelle2>
   </export_x005F_xml_message>
   <export_x005F_xml_message>
      <libelle><![CDATA[h euismod tincidunt ut lao]]></libelle>
      <price>200</price>
      <libelle2><![CDATA[h euirci tation ullamcorper]]></libelle2>
   </export_x005F_xml_message>
</dataroot>

Explanation:

Proper use of the cdata-section-elements attribute of xsl:output .

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
1

Well, the only problem I see is the call of the xsl-template.

It should look like this:

<xsl:template name="MyTemplateName">
    <someTag>
        <xsl:text disable-output-escaping="yes">
                    &lt;![CDATA[
            </xsl:text>

        <someOtherTag/>

        <xsl:text disable-output-escaping="yes">
                 ]]&gt;
            </xsl:text>
    </someTag>
</xsl:template>

So your template would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd">

<xsl:template match="/">
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata"  generated="2012-07-31T13:25:46">
    <xsl:apply-templates select="export_x005F_xml_message"/>
    </dataroot>
</xsl:template>

<xsl:template match="export_x005F_xml_message">
    <export_x005F_xml_message>
        <libelle>
        <xsl:text disable-output-escaping="yes">
         &lt;![CDATA[
       </xsl:text>
            <xsl:value-of select="libelle"/>
        </libelle>
        <xsl:text disable-output-escaping="yes">
        ]]&gt;
       </xsl:text>
       ...
    </export_x005F_xml_message>
</xsl:template>

</xsl:stylesheet>
Leonkur
  • 71
  • 3