Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering XML & Transforming To HTML 1

Status
Not open for further replies.

lbarron

Technical User
Jan 22, 2002
92
GB
Hi Group,

I have an XML file that I am filtering using the SelectSingleNode method. I am them transforming the filtered XML into HTML using a style sheet.

The results that are sent to the client are not formatted to the style sheet, one long continuous string of data is displayed instead.

Is this something to do with the SelectSingleNode method returning a NodeElement? or is it just my code :)

Any suggestions would be great.

I am using ASP,VBScript,MSXML3.0 SP2 in replace mode.

TIA

Lee.
 
It looks like your stylesheet fails. Perhaps if you explain where you perform the transformation (server side or do you open the filtered xml in the browser?) and post some xsl code and a sample of your xml we could be of more assistance.

Jordi Reineman
 
Jordi,

Thanks for replying.

I am performing the translation on the server side using ASP & MSXML.

I will post my XML/XSL tomorrow when I get back to the office.

Thanks again.

Lee.
 
Jordi,

My ASP Code, XML & XSL is shown below.

I am trying to filter the XML on a particular Order (OurOrder) and then transform this to HTML for displaying by the client.

Hope this helps!

Thanks

Lee.

---

My sample XML file is as follows :-

<?xml version=&quot;1.0&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot;?>
<OrderStatus>
<OrderDetails>
<OurOrder>99999</OurOrder>
<CustomerOrder>TEST 1</CustomerOrder>
<OrderDate>12/05/1998</OrderDate>
<CustomerDetails>
<AccountRef>XXXX</AccountRef>
<CustomerName>XXXXXXX</CustomerName>
</CustomerDetails>
</OrderDetails>
<OrderDetails>
<OurOrder>88888</OurOrder>
<CustomerOrder>XXXXXXX</CustomerOrder>
<OrderDate>26/08/1998</OrderDate>
<CustomerDetails>
<AccountRef>ZZZZZZ</AccountRef>
<CustomerName>ZZZZZZZZZZZZZZZZZZZ</CustomerName>
</CustomerDetails>
</OrderDetails>
</OrderStatus>

My ASP Code Is As Follows :-

<%@ LANGUAGE = VBScript %>
<%

' Define The Source XML & XSL Documents.

xmlfile = Server.MapPath(&quot;OrderStatus.xml&quot;)
styleFile = Server.MapPath(&quot;OrderStatus.xsl&quot;)

' Instantiate A Document Object For Our XML File.

set source = Server.CreateObject &quot;MSXML2.FreeThreadedDOMDocument&quot;)source.async = false

source.load(xmlfile)

' Instantiate A Document Object For Our XSL File.

set style = Server.CreateObject&quot;MSXML2.FreeThreadedDOMDocument&quot;)
style.async = false
style.load(styleFile)

' Filter The XML.

set order = source.selectSingleNode(&quot;/OrderStatus/OrderDetails[OurOrder = 'T/02222']&quot;)

' Transform The XML In To HTML Using Our XSL.

Response.Write(order.transformNode(style))
%>

My XSL Is as follows :-

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:eek:utput method=&quot;html&quot;/>
<xsl:template match=&quot;/&quot;>
<HTML>
<HEAD>
<TITLE>Order Status Report</TITLE>
</HEAD>
<BODY>
<h1><xsl:value-of select=&quot;OurOrder&quot;/></h1>
<h2><xsl:value-of select=&quot;CustomerOrder&quot;/></h2>
<h2><xsl:value-of select=&quot;OrderDate&quot;/></h2>
<h3><xsl:value-of select=&quot;CustomerDetails/AccountRef&quot;/></h3>
<h3><xsl:value-of select=&quot;CustomerDetails/CustomerName&quot;/></h3>
<table>
<th>Our Order #</th>
<th>Quality</th>
<th>HT Desc</th>
<th>Weight</th>
<th>Planned Despatch</th>
<th>Revised Despatch</th>
<th>Planned Cast</th>
<th>Actual Cast</th>
<th>Present Station</th>
<th>Machining Completed</th>
<xsl:for-each select=&quot;//OrderStatus/OrderDetails/RollDetails&quot;>
<tr>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;RollNo&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;Quality&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;HTDesc&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;Weight&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;PlannedDesp&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;RevisedDesp&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;PlannedCast&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;ActualCast&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;Station&quot;/></td>
<td style=&quot;text-align:center&quot;><xsl:value-of select=&quot;MachineComp&quot;/></td>
</tr>
</table>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
 
Jordi,

I have just noticed a typo in the last post I did!

When I am doing the selectSingleNode our order should be 99999 and not T/02222.

Lee.
 
Lee,

The code looks correct at first glance. Your stylesheet however.....

You should consider that xsl is a context related language. So if the context is on the OrderDetails node calling <xsl:value-of select=&quot;OrderDate&quot;> will produce nothing (there is no Orderdate node directly beneath the OrderDetails node). Calling it according to the context:
<xsl:value-of select=&quot;OurOrder/OrderDate&quot;> however will result in data.

Furthermore you match the root node (/). Remember that the root node isn't represented by anything in your xmldata. The root of your xml document doesn't equal OurOrder!!

Furthermore you perform a <xsl:for-each select=&quot;//OrderStatus/OrderDetails/RollDetails&quot;>, but because of the SelectSingleNode call you don't even have any OrderStatus nodes in your xml file.

To keep your code readable I should suggest you use named templates to separate certain kinds of functionality.

Perhaps the problem lies somewhere else, but you should begin with designing a fault free stylesheet.

Develop your stylesheet one step at a time and test it!!

Good luck!

Jordi Reineman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top