XSL Transformation解析同名列

时间:2018-03-28 05:30:19

标签: xml xslt soap cdata envelope

我有xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <processOCIMessageResponse xmlns="urn:com:broadsoft:webservice">
            <processOCIMessageReturn><![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
                <MyDocument protocol="OCI" xmlns="C" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <sessionId xmlns="">12345</sessionId>
                    <command echo="" xsi:type="AuthenticationResponse" xmlns="">
                        <userId>user_test</userId>
                    </command>
                    <command echo="" xsi:type="ServiceProvider" xmlns="">
                        <ProviderTable>
                            <colHeading>Service Provider Id</colHeading>
                            <colHeading>Service Provider Name</colHeading>
                            <colHeading>Is Enterprise</colHeading>
                            <row><col>7163939_id</col><col/><col>true</col></row>
                            <row><col>3134473_build</col><col/><col>true</col></row>
                            <row><col>11421427_group</col><col/><col>true</col></row>
                        </ProviderTable>
                    </command>
                    </MyDocument>]]>
            </processOCIMessageReturn>
        </processOCIMessageResponse>
    </soapenv:Body>
</soapenv:Envelope>

如何从col标签中提取值?因为标签在一行中重复多次。

这是我到目前为止所做的:

<xsl:for-each select="./command/ProviderTable/row">
    <xsl:element name="SERVICE_PROVIDER">
        <SERVICE_PROVIDER_ID>
            <xsl:value-of select="col"/>
        </SERVICE_PROVIDER_ID>
        <SERVICE_PROVIDER_NAME>
            <xsl:value-of select="col"/>
        </SERVICE_PROVIDER_NAME>
        <IS_ENTERPRISE>
            <xsl:value-of select="col">
        </IS_ENTERPRISE>
    </xsl:element>
</xsl:for-each>

预期产出

<?xml version="1.0"?>
<main>
    <ProviderTable>
        <SERVICE_PROVIDER_ID>7163939_id</SERVICE_PROVIDER_ID>
        <SERVICE_PROVIDER_NAME></SERVICE_PROVIDER_NAME>
        <IS_ENTERPRISE>true</IS_ENTERPRISE>
    </ProviderTable>
    <ProviderTable>
        <SERVICE_PROVIDER_ID>3134473_build</SERVICE_PROVIDER_ID>
        <SERVICE_PROVIDER_NAME></SERVICE_PROVIDER_NAME>
        <IS_ENTERPRISE>true</IS_ENTERPRISE>
    </ProviderTable>
    <ProviderTable>
        <SERVICE_PROVIDER_ID>11421427_group</SERVICE_PROVIDER_ID>
        <SERVICE_PROVIDER_NAME></SERVICE_PROVIDER_NAME>
        <IS_ENTERPRISE>true</IS_ENTERPRISE>
    </ProviderTable>
</main>

1 个答案:

答案 0 :(得分:0)

如果每个<col>的{​​{1}}元素数始终为3,则可以使用<row> index元素来访问这些值。 XSLT中的索引从<col>开始,即1col[1],依此类推。

col[2]可以修改如下。

<xsl:for-each>