Search code examples
xmlxsltsap-basis

Problem with XML(ATOM) from SAP using XSL to Display Multiple Attributes(within same element) in a HTML table


Hi I am very new to XML.

I am trying to transform XML to HTML using XSL. Internet Explorer keeps giving a blank display. The XML has been generated by a SAP demonstration system I have built for practice. The result I am expecting is a HTML table that will hold the 6 or so attributes of each element in a row.

XML below:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/Protocols/SAPData" Version="1.0">
<edmx:DataServices m:DataServiceVersion="2.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="ZSFLIGHT_PROJECT_SRV" xml:lang="en" sap:schema-version="1">
<?xml-stylesheet type="text/xsl" href="xml_from_sap.xsl"?>



<EntityType  Name="Carrier" sap:content-version="1">

<Key>
<PropertyRef Name="Carrid"/>
</Key>

<Property Name="Mandt" Type="Edm.String" Nullable="false" MaxLength="3" sap:unicode="false" sap:label="Client" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
<Property Name="Carrid" Type="Edm.String" Nullable="false" MaxLength="3" sap:unicode="false" sap:label="Airline" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
<Property Name="Carrname" Type="Edm.String" Nullable="false" MaxLength="20" sap:unicode="false" sap:label="Airline" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
<Property Name="Currcode" Type="Edm.String" Nullable="false" MaxLength="5" sap:unicode="false" sap:label="Airline Currency" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false" sap:semantics="currency-code"/>
<Property Name="Url" Type="Edm.String" Nullable="false" MaxLength="255" sap:unicode="false" sap:label="URL" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
</EntityType>


<EntityContainer Name="ZSFLIGHT_PROJECT_SRV_Entities" m:IsDefaultEntityContainer="true" sap:supported-formats="atom json xlsx">
<EntitySet Name="CarrierSet" EntityType="ZSFLIGHT_PROJECT_SRV.Carrier" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:pageable="false" sap:addressable="false" sap:content-version="1"/>
<EntitySet Name="Carriers" EntityType="ZSFLIGHT_PROJECT_SRV.Carrier" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:pageable="false" sap:addressable="false" sap:content-version="1"/>
</EntityContainer>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="self" href="http://vhcalnplci:8000/sap/opu/odata/sap/ZSFLIGHT_PROJECT_SRV/$metadata"/>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="latest-version" href="http://vhcalnplci:8000/sap/opu/odata/sap/ZSFLIGHT_PROJECT_SRV/$metadata"/>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

XSL Below:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h2>SAP Flights</h2>
            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>Sytem Name</th>
                  <th>Carrier ID</th>
                  <th>Carrier Name</th>
                  <th>Currency Code</th>
                  <th>URL</th>
               </tr>

               <xsl:for-each select = "EntityType/Property"> 
                  <tr>
                     <td><xsl:value-of select = "@Name"/></td>
                     <td><xsl:value-of select = "@Type"/></td>
                     <td><xsl:value-of select = "@Nullable"/></td>
                     <td><xsl:value-of select = "@MaxLength"/></td>
                     <td><xsl:value-of select = "@Name"/></td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>



Solution

  • You have to take into account the namespace of the elements in your XML file. Here's how you could do this:

    <?xml version = "1.0" encoding = "UTF-8"?>
    <xsl:stylesheet version = "1.0"
       xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
       xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
       xmlns:edm="http://schemas.microsoft.com/ado/2008/09/edm"
       exclude-result-prefixes="edm edmx">
    
      <xsl:output method="html" indent="yes"/>
    
       <xsl:template match = "/">
          <html>
             <body>
                <h2>SAP Flights</h2>
                <table border = "1">
                   <tr bgcolor = "#9acd32">
                      <th>Sytem Name</th>
                      <th>Carrier ID</th>
                      <th>Carrier Name</th>
                      <th>Currency Code</th>
                      <th>URL</th>
                   </tr>
    
                   <xsl:for-each select = "edmx:Edmx/edmx:DataServices/edm:Schema/edm:EntityType/edm:Property">
                       <tr><td>Here</td></tr>
                      <tr>
                         <td><xsl:value-of select = "@Name"/></td>
                         <td><xsl:value-of select = "@Type"/></td>
                         <td><xsl:value-of select = "@Nullable"/></td>
                         <td><xsl:value-of select = "@MaxLength"/></td>
                         <td><xsl:value-of select = "@Name"/></td>
                      </tr>
                   </xsl:for-each>
                </table>
             </body>
          </html>
       </xsl:template>
    </xsl:stylesheet>
    

    See it working here : https://xsltfiddle.liberty-development.net/bEzknt4

    As to why you were not seeing any output, how are you running your XSLT transformation?