1

I am trying to transform an xml from one xml to another by using xslt, but not able to get the error.

My input is :

<?xml version = "1.0"?> 
<queryResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">
    <result>
        <sfobject>
            <id>1791
            </id>
            <type>CompoundEmployee
            </type>
            <execution_timestamp>2016-11-08T06:38:48.000Z
            </execution_timestamp>
            <version_id>1611P0
            </version_id>
        </sfobject>     
        <sfobject>
            <id>122
            </id>
            <type>Simple
            </type>
            <execution_timestamp>2016-11-08T08:32:18.000Z
            </execution_timestamp>
            <version_id>16120
            </version_id>
        </sfobject>
        <numResults>1
        </numResults>
        <hasMore>true
        </hasMore>
        <querySessionId>5f619648-548a-43ec-8119-627094f927a5
        </querySessionId>
    </result>
</queryResponse>

and the XSLT is:

<?xml version = "1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="result">
        <calls>
            <xsl:for-each select = "sfobject">
                <call>
                    <id>
                        <xsl:value-of select = "id"/>
                    </id>
                    <type>
                        <xsl:value-of select = "type"/>
                    </type>
                </call>
            </xsl:for-each>     
        </calls>
    </xsl:template>
</xsl:stylesheet>

I am receiving the output as below:

1791 CompoundEmployee 2016-11-08T06:38:48.000Z 1611P0 122 Simple
2016-11-08T08:32:18.000Z 16120 1 true
5f619648-548a-43ec-8119-627094f927a5

Basically, I don't get the tags. I intend to get the output as follows:

<calls>
  <call>
    <id>123</id>
    <type>CompoundEmployee</type>
  </call>
</calls>
zx485
  • 28,498
  • 28
  • 50
  • 59

2 Answers2

0

You need to give namespace in your xslt file and access your element with namespace prefix

Your xslt file should be

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
               xmlns:sf="urn:sfobject.sfapi.successfactors.com"
               xmlns:sin="urn:fault.sfapi.successfactors.com" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
               exclude-result-prefixes="sf sin xs">
<xsl:output indent="yes" method="xml" />
   <xsl:template match="/sf:queryResponse">
      <calls>
         <xsl:for-each select="sf:result/sf:sfobject">
            <call>
               <id>
                  <xsl:value-of select="sf:id" />
               </id>
               <type>
                  <xsl:value-of select="sf:type" />
               </type>
            </call>
         </xsl:for-each>
      </calls>
   </xsl:template>
</xsl:stylesheet>
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
-1

In the first line of your XML

<queryResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">

you set your XML default namespace to

xmlns="urn:sfobject.sfapi.successfactors.com"

which results in the <result> nodes really being seen as

<urn:sfobject.sfapi.successfactors.com:result>

nodes. So the following line in your XSLT

<xsl:template match="result">

tries to match <result> nodes, but merely finds

<urn:sfobject.sfapi.successfactors.com:result> 

nodes in your XML file. Hence the "no-matches" text output.

Removing the

xmlns="urn:sfobject.sfapi.successfactors.com"

from the first line of your XML file would solve the problem.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • The XSLT language can process any well-formed XML input. There is no need to remove the namespace declaration or modify the input in any other way. – michael.hor257k Nov 08 '16 at 13:55