Solved

Different XML Formats?

Posted on 2003-11-24
4
670 Views
Last Modified: 2013-11-19
I have a standard XML file as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="DataContent.xsl"?>
<DataContent>
  <person>
    <name>Sam the Clam</name>
    <detail>Sea Dweller</detail>
    <age>39</age>
  </person>
  <person>
    <name>Fred the Fish</name>
    <detail>Sea Dweller</detail>
    <age>18</age>
  </person>
</DataContent>

and if I use the following XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
  <html>
  <body>
    <h2>Data Content Display</h2>
    <table border="0" cellspacing="2">
    <tr bgcolor="gray">
      <th align="left" width="100">Name</th>
      <th align="left" width="150">Detail</th>      
        <th align="left" width="50">Age</th>
    </tr>
    <xsl:for-each select="DataContent/person">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="detail"/></td>      
        <td><xsl:value-of select="age"/></td>
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template></xsl:stylesheet>

I can create an ASP page as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
  sourceFile = Server.MapPath("DataContent.xml")
  styleFile = Server.MapPath("DataContent.xsl")
 
  set source = Server.CreateObject("Microsoft.XMLDOM")
  source.async = false
  source.load(sourceFile)
  set style = Server.CreateObject("Microsoft.XMLDOM")
  style.async = false
  style.load(styleFile)
  Response.Write source.transformNode(style)
%>


However, if I use ADO to create my XML:

objRS.save strPath adPersistXML

then the XML looks different like this example from the Northwind database:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
      xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
      xmlns:rs='urn:schemas-microsoft-com:rowset'
      xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
      <s:ElementType name='row' content='eltOnly' rs:updatable='true'>
            <s:AttributeType name='CompanyName' rs:number='1' rs:nullable='true' rs:maydefer='true' rs:write='true' rs:basetable='Customers'
                   rs:basecolumn='CompanyName'>
                  <s:datatype dt:type='string' dt:maxLength='40'/>
            </s:AttributeType>
            <s:AttributeType name='ContactName' rs:number='2' rs:nullable='true' rs:maydefer='true' rs:write='true' rs:basetable='Customers'
                   rs:basecolumn='ContactName'>
                  <s:datatype dt:type='string' dt:maxLength='30'/>
            </s:AttributeType>
            <s:AttributeType name='Phone' rs:number='3' rs:nullable='true' rs:maydefer='true' rs:write='true' rs:basetable='Customers'
                   rs:basecolumn='Phone'>
                  <s:datatype dt:type='string' dt:maxLength='24'/>
            </s:AttributeType>
            <s:extends type='rs:rowbase'/>
      </s:ElementType>
</s:Schema>
<rs:data>
      <z:row CompanyName='Alfreds Futterkiste' ContactName='Maria Anders' Phone='030-0074321'/>
      <z:row CompanyName='Ana Trujillo Emparedados y helados' ContactName='Ana Trujillo' Phone='(5) 555-4729'/>
      <z:row CompanyName='Antonio Moreno Taquería' ContactName='Antonio Moreno' Phone='(5) 555-3932'/>
      <z:row CompanyName='Around the Horn' ContactName='Thomas Hardy' Phone='(171) 555-7788'/>
      <z:row CompanyName='Berglunds snabbköp' ContactName='Christina Berglund' Phone='0921-12 34 65'/>

</rs:data>
</xml>

So now for the question: how do I work with this second XML format to accomplish the same thing? For your answer, please keep in mind that I am very new to XML/XSL and will need a little hand holding.

Thanks,

Fritz the Blank
0
Comment
Question by:fritz_the_blank
  • 2
4 Comments
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
ID: 9811332
There are some very capable people in this Topic Area, so no doubt they will give you the best solution, but in any case just so that I understand, there are a couple of solutions:

1. You want an XSLT to transform the first XML document, qhich will be subsequently transformed again by the XSLT listed above.

2. (Recommended) You want to use the XML generated from the recordset and transform it into the same HTML result as currently generated by the current XML/XSLT listed above.  And in consequence bypass the XML/XSLT listed.

Anthony
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 9811389
Let's go with (2)

I tried modifying my xsl file to match the field names, but it always returned an empty table--no errors but no data...

FtB
0
 
LVL 9

Accepted Solution

by:
sparkplug earned 400 total points
ID: 9811471
Hi,

To change your XSLT, first we need to add the namespaces defined in the XML to the XSLT file. The namespaces define the prefixes used such as s: and z:.

So the first line of the XSLT should look like:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" exclude-result-prefixes="s rs z">


Next we need to change the for-each loop so that it selects the correct nodes. We need to select each <z:row> element, therefore we change:

<xsl:for-each select="DataContent/person">

to

<xsl:for-each select="xml/rs:data/z:row">


Now for the table cells; Instead of:

<tr>
     <td><xsl:value-of select="name"/></td>
     <td><xsl:value-of select="detail"/></td>      
     <td><xsl:value-of select="age"/></td>
</tr>

we need to select each of the attributes of the <z:row> element:

<tr>
     <td><xsl:value-of select="@CompanyName"/></td>
     <td><xsl:value-of select="@ContactName"/></td>      
     <td><xsl:value-of select="@Phone"/></td>
</tr>


Note the use of @ to select attributes.

We also need to change the headers for each of the columns. The full XSLT is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" exclude-result-prefixes="s rs z">
      <xsl:template match="/">
            <html>
                  <body>
                        <h2>Data Content Display</h2>
                        <table border="0" cellspacing="2">
                              <tr bgcolor="gray">
                                    <th align="left" width="100">Company Name</th>
                                    <th align="left" width="150">Contact Name</th>
                                    <th align="left" width="50">Phone</th>
                              </tr>
                              <xsl:for-each select="xml/rs:data/z:row">
                                    <tr>
                                          <td>
                                                <xsl:value-of select="@CompanyName"/>
                                          </td>
                                          <td>
                                                <xsl:value-of select="@ContactName"/>
                                          </td>
                                          <td>
                                                <xsl:value-of select="@Phone"/>
                                          </td>
                                    </tr>
                              </xsl:for-each>
                        </table>
                  </body>
            </html>
      </xsl:template>
</xsl:stylesheet>


I see also that you are using "Microsoft.XMLDOM" in you ASP code. This refers to an old version of the XML parser. I recommend that you change this to "MSXML2.DOMDocument" as this will offer far better performance.

e.g. set source = Server.CreateObject("MSXML2.DOMDocument")


Hope this helps.

>S'Plug<



0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 9811534
Thank you to both!!!

I am going to study this carefully to see what I can learn.

Once I have a working example, I find that following tutorials and the like is a whole lot easier.


FtB
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSS HELP 6 84
Unable to resolve XML http request 4 75
How to replace a token in a string with square brackets? 2 55
Create XML 5 46
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now