简体   繁体   中英

Cannot transform from one xml file to another xml via xslt

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>

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM