使用XSL从CDATA转换XML

时间:2013-09-04 11:29:53

标签: xslt xml-parsing

我有这个XML文档:

<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>

我如何使用XSL解析它以获取CDATA中的每一行来进行这种选择:

<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>

2 个答案:

答案 0 :(得分:1)

正如评论中所建议的,我发布了一个解决方案。该示例使用XSLT 3.0与Saxon 9.5(需要PE或EE版本):

<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>

输入

<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>

结果是

<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>

答案 1 :(得分:0)

如果您有exslt个扩展程序,则应该可以使用exslt:node-set创建内容变量。尝试类似(未经测试的):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:exsl="http://exslt.org/common"
   extension-element-prefixes="exsl"
   exclude-result-prefixes="exsl"
   version="1.0">

<xsl:template match="/">
    <xsl:variable name="inner" select="exsl:node-set(//data/text())" />
    <select name="bank" id="bank" class="input" style="width: 370px;">
        <xsl:for-each select="$inner//row">
            ...
        </xsl:for-each>
    </select>
</xsl:template>

</xsl:stylesheet>