1

I have used an XSD to define the schema from the XML file.

Customer.xsd:

An XML Schema Documentation to describe a customer details

<xs:import namespace="http://Address" schemaLocation="file:///C:/Users/SaithKumar/Documents/Altova/XMLSpy2017/Examples/AddressDetails.xsd "/>


<xs:element name="Persons">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Customer" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="firstname" type="xs:string"/>
                        <xs:element name="lastname" type="xs:string"/>
                        <xs:element name="nickname" type="xs:string"/>
                        <xs:element name="phone" type="xs:float"/>
                        <xs:element ref="add:Address"/>
                        <xs:element name="Shipping_info">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="tracking_no" type="xs:string"/>
                                    <xs:element name="delivery_no" type="xs:string" minOccurs="0"/>
                                    <xs:element name="type" type="xs:string"/>
                                    <xs:element name="delivery_date" type="xs:string"/>
                                    <xs:element ref="add:Address"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="Payment">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="cardno" type="xs:string"/>
                                    <xs:element name="cvv" type="xs:int"/>
                                    <xs:element name="date" type="xs:string"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="id" type="xs:int"/>
                </xs:complexType>
            </xs:element>               
        </xs:sequence>
    </xs:complexType>
</xs:element>

Address Schema for the above XSD file is imported from this file Address.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"      xmlns:add="http://Address" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://Address" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:annotation>
    <xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:element name="Address">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="addr1" type="xs:string"/>
            <xs:element name="addr2" type="xs:string" minOccurs="0"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="state" type="xs:string"/>
            <xs:element name="zipcode" type="xs:int"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

XSL file is displayed here.

CustomersXSL

 <?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>
        <h3>Details of each Students. Xpath expression = "/Persons"</h3>
        <h3>Details of each Students. Xpath expression = "/Persons/*"</h3>

        <table border = "1">
           <tr bgcolor = "#9acd32">
              <th>Roll No</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Nick Name</th>
              <th>Phone</th>
           </tr>

           <xsl:for-each select = "/Persons/*">
              <tr>
                 <td><xsl:value-of select = "@id"/></td>
                 <td><xsl:value-of select = "firstname"/></td>
                 <td><xsl:value-of select = "lastname"/></td>
                 <td><xsl:value-of select = "nickname"/></td>
                 <td><xsl:value-of select = "phone"/></td>
              </tr>
           </xsl:for-each>
        </table> 

        <h3>Details of each Students. Xpath expression = "/Persons/Customer"</h3>

        <table border = "1">
           <tr bgcolor = "#9acd32">
              <th>Roll No</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Nick Name</th>
              <th>Phone</th>
           </tr>    

           <xsl:for-each select = "/Persons/Customer">
              <tr>
                 <td><xsl:value-of select = "@id"/></td>
                 <td><xsl:value-of select = "firstname"/></td>
                 <td><xsl:value-of select = "lastname"/></td>
                 <td><xsl:value-of select = "nickname"/></td>
                 <td><xsl:value-of select = "phone"/></td>
              </tr>
           </xsl:for-each>
        </table> 

        <h3>Details of each Students. Xpath expression = "//Customer"</h3>

        <table border = "1">
           <tr bgcolor = "#9acd32">
              <th>Roll No</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Nick Name</th>
              <th>Phone</th>
           </tr>    

           <xsl:for-each select = "//Customer">
              <tr>
                 <td><xsl:value-of select = "@id"/></td>
                 <td><xsl:value-of select = "firstname"/></td>
                 <td><xsl:value-of select = "lastname"/></td>
                 <td><xsl:value-of select = "nickname"/></td>
                 <td><xsl:value-of select = "phone"/></td>
              </tr>
           </xsl:for-each>
        </table> 

     </body>
  </html>

and finally the XML file for all this id displayed here. CustomersXML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "CustomersXSL2.xsl"?>
<Persons xmlns="http://customers" xmlns:add="http://Address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://customers file:///C:/Users/SaithKumar/Documents/Altova/XMLSpy2017/Examples/CustomersXSD2.xsd">
<Customer id="561">
    <firstname>Saith Kumar</firstname>
    <lastname>Gundu</lastname>
    <nickname>sgundu</nickname>
    <phone>9805621459</phone>
    <add:Address>
        <add:addr1>9303 Kittansett Drive</add:addr1>
        <add:city>Charlotte</add:city>
        <add:state>NC</add:state>
        <add:zipcode>28262</add:zipcode>
    </add:Address>
    <Shipping_info>
        <tracking_no>800910872</tracking_no>
        <type>UPS</type>
        <delivery_date>02/10/2017</delivery_date>
        <add:Address>
            <add:addr1>9303 Kittansett Drive</add:addr1>
            <add:city>Charlotte</add:city>
            <add:state>NC</add:state>
            <add:zipcode>28262</add:zipcode>
        </add:Address>
    </Shipping_info>
    <Payment>
        <cardno>4400556688997564</cardno>
        <cvv>869</cvv>
        <date>04/19</date>
    </Payment>
</Customer>
<Customer id="562">
    <firstname>Mano Vikas</firstname>
    <lastname>Banavathu</lastname>
    <nickname>mbanavat</nickname>
    <phone>7596241235</phone>
    <add:Address>
        <add:addr1>9303 Kittansett Drive</add:addr1>
        <add:city>Charlotte</add:city>
        <add:state>NC</add:state>
        <add:zipcode>28262</add:zipcode>
    </add:Address>
    <Shipping_info>
        <tracking_no>800942689</tracking_no>
        <type>FedEx</type>
        <delivery_date>29/01/2017</delivery_date>
        <add:Address>
            <add:addr1>9306 Kittansett Drive</add:addr1>
            <add:city>Charlotte</add:city>
            <add:state>NC</add:state>
            <add:zipcode>28262</add:zipcode>
        </add:Address>
    </Shipping_info>
    <Payment>
        <cardno>6897562456895625</cardno>
        <cvv>568</cvv>
        <date>04/59</date>
    </Payment>
</Customer>
<Customer id="563">
    <firstname>Himanshu</firstname>
    <lastname>Agarwal</lastname>
    <nickname>hagarw</nickname>
    <phone>9804455010</phone>
    <add:Address>
        <add:addr1>9303 Kittansett Drive</add:addr1>
        <add:city>Charlotte</add:city>
        <add:state>NC</add:state>
        <add:zipcode>28262</add:zipcode>
    </add:Address>
    <Shipping_info>
        <tracking_no>800910845</tracking_no>
        <type>USPS</type>
        <delivery_date>5/25/2016</delivery_date>
        <add:Address>
            <add:addr1>9303 Kittansett Drive</add:addr1>
            <add:city>Charlotte</add:city>
            <add:state>NC</add:state>
            <add:zipcode>28262</add:zipcode>
        </add:Address>
    </Shipping_info>
    <Payment>
        <cardno>8654896589638523</cardno>
        <cvv>741</cvv>
        <date>05/21</date>
    </Payment>
</Customer>

All the code displayed above is written in Altova XMLSPY and it validated all the files. Yet when I run the XML file in the browser the table from the stylesheet is being displayed but only with the header row. The data is not being displayed. Can you please tell me where I am doing wrong? Thanks.

  • 1
    The schema documents are irrelevant here. Your problem is the XML's default namespace - see: http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k Feb 11 '17 at 02:09

0 Answers0