Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform XML from CDATA using XSL

I have this XML document:

<ns0:getDataResponse xmlns:ns0="http://abc.com/">
    <return>
        <wrapper>
            <data><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
                            <ConDic>
                            <dictionary>bank</dictionary>
                                <rows>
                                    <row>
                                      <bic>ABKZKZKX</bic>
                                      <bcode>319</bcode>
                                      <name1>AA &quot;A BANK&quot;</namekz>
                                      <name2>BB &quot;B BANK&quot;</nameru>
                                    </row>
                                    <row>
                                      <bic>ABNAKZKX</bic>
                                      <bcode>922</bcode>
                                      <name1>CC &quot;C BANK&quot;</namekz>
                                      <name2>DD &quot;D BANK&quot;</nameru>
                                    </row>
                                </rows>
                            </ConDic>]]></data>
        </wrapper>
    </return>
</ns0:getDataResponse>

How I can parse it with XSL to get each rows in CDATA to make this kind of select:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://abc.com/">
<xsl:output method="html" />
<xsl:template match="text()|@*"/>
<xsl:template match="ns0:rows">

<select name="bank" id="bank" class="input" style="width: 370px;">
    <xsl:for-each select="row">
        <xsl:sort select="name1"/>
        <option value="{bic}"><xsl:value-of select="name1" /></option>
    </xsl:for-each>
</select>
like image 975
Eazy Avatar asked Sep 04 '13 11:09

Eazy


People also ask

What is CDATA in XSL?

The term CDATA, meaning character data, is used for distinct, but related, purposes in the markup languages SGML and XML. The term indicates that a certain portion of the document is general character data, rather than non-character data or character data with a more specific, limited structure.

Can XSLT transform XML to JSON?

JSON is a lightweight data-interchange format based on a subset of the JavaScript language, and often offered as an alternative to XML in—for example—web services. To make life easier XSLTJSON allows you to transform XML to JSON automatically.

Can XSLT transform XML to CSV?

This post shows you how to convert a simple XML file to CSV using XSLT. The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.


1 Answers

As suggested in a comment, I post a solution. The example use XSLT 3.0 with Saxon 9.5 (needs PE or EE version):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ns0="http://abc.com/"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math ns0"
    version="3.0">


    <xsl:output method="html" indent="yes"/>

    <xsl:template match="text()|@*"/>

    <xsl:template match="ns0:getDataResponse">

        <select name="bank" id="bank" class="input" style="width: 370px;">
            <xsl:for-each select="parse-xml(return/wrapper/data)//row">
                <xsl:sort select="name1"/>
                <option value="{bic}">
                    <xsl:value-of select="name1"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>

</xsl:stylesheet>

With an input of

<ns0:getDataResponse xmlns:ns0="http://abc.com/">
    <return>
        <wrapper>
            <data><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
                            <ConDic>
                            <dictionary>bank</dictionary>
                                <rows>
                                    <row>
                                      <bic>ABKZKZKX</bic>
                                      <bcode>319</bcode>
                                      <name1>AA &quot;A BANK&quot;</name1>
                                      <name2>BB &quot;B BANK&quot;</name2>
                                    </row>
                                    <row>
                                      <bic>ABNAKZKX</bic>
                                      <bcode>922</bcode>
                                      <name1>CC &quot;C BANK&quot;</name1>
                                      <name2>DD &quot;D BANK&quot;</name2>
                                    </row>
                                </rows>
                            </ConDic>]]></data>
        </wrapper>
    </return>
</ns0:getDataResponse>

the result is

<select name="bank" id="bank" class="input" style="width: 370px;">
   <option value="ABKZKZKX">AA "A BANK"</option>
   <option value="ABNAKZKX">CC "C BANK"</option></select>
like image 139
Martin Honnen Avatar answered Sep 22 '22 12:09

Martin Honnen