Transform XML from CDATA using XSL

前端 未结 2 1729
南旧
南旧 2021-01-17 17:31

I have this XML document:


    
        
            

        
相关标签:
2条回答
  • 2021-01-17 17:56

    If you have the exslt extensions available, you should be able to use exslt:node-set to create a variable of the contents. Try something like (untested):

    <?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>
    
    0 讨论(0)
  • 2021-01-17 18:00

    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>
    
    0 讨论(0)
提交回复
热议问题