-1

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

how I can extract values from col tag? because The tag is repeated several times in one line.

This is what I have so far:

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

Expected Output

<?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>
Aniket V
  • 3,183
  • 2
  • 15
  • 27
Alex
  • 1
  • 2

1 Answers1

0

If the number of <col> elements for every <row> is always going to be 3, you can use the index for the <col> elements to access the values. Index in XSLT start from 1 i.e. col[1], col[2] and so on.

The <xsl:for-each> can be modified as below.

<xsl:for-each select="./command/ProviderTable/row">
    <xsl:element name="SERVICE_PROVIDER">
        <SERVICE_PROVIDER_ID>
            <xsl:value-of select="col[1]"/>
        </SERVICE_PROVIDER_ID>
        <SERVICE_PROVIDER_NAME>
            <xsl:value-of select="col[2]"/>
        </SERVICE_PROVIDER_NAME>
        <IS_ENTERPRISE>
            <xsl:value-of select="col[3]">
        </IS_ENTERPRISE>
    </xsl:element>
</xsl:for-each>
Aniket V
  • 3,183
  • 2
  • 15
  • 27
  • What did not work? Please share the entire XSLT which will give an idea of what you are trying. – Aniket V Mar 28 '18 at 16:44
  • Didn't work col[1] etc... template is too long. There is error: `Array ( [type] => 2 [message] => XSLTProcessor::transformToXml(): No stylesheet associated to this object [file] => /var/www/html/online-toolz.com/functions/XSLT.php [line] => 26 ) Error:XSLTProcessor::transformToXml(): No stylesheet associated to this object` – Alex Mar 28 '18 at 17:35