Help w/ XSLT

Published Sun, Nov 14 2004 17:02 | William

Ok, what I'm trying to do is so simple that some 8th grader will probably answer this in 5 seconds and make me look like a big dummy.  I have XML from a Dataset that's being piped into an XML Control on an ASP.NET Page.  The document structure looks like this:

- <dsShoppingCartItem xmlns="http://tempuri.org/dsShoppingCartItem.xsd">
- <Location>
  <LocationId>1</LocationId>
  <LocationTypeSId>GENERAL</LocationTypeSId>
  <DBAName>Bill is as dumb as a Rock</DBAName>
  <TotalAmount>160</TotalAmount>
  </Location>
- <Location>
  <LocationId>2</LocationId>
  <LocationTypeSId>LOCATION</LocationTypeSId>
  <DBAName>Some Company, LTD.</DBAName>
  <TotalAmount>245.12</TotalAmount>
  </Location>
- <ShoppingCartItem>
  <LocationId>1</LocationId>
  <ShoppingCartItemId>1</ShoppingCartItemId>
  <WorkflowSId>Whatever</WorkflowSId>
  <WorkflowName>Start New Business</WorkflowName>
  <WizardStateId>1</WizardStateId>
  <CreateDate>2004-08-03T00:00:00.0000000-04:00</CreateDate>
  <TotalAmount>160</TotalAmount>
  <PaymentRejected>False</PaymentRejected>
  <CheckPay>false</CheckPay>
  </ShoppingCartItem>
- <ShoppingCartItem>
  <LocationId>2</LocationId>
  <ShoppingCartItemId>2</ShoppingCartItemId>
  <WorkflowSId>Strip Club License</WorkflowSId>
  <WorkflowName>Club License</WorkflowName>
  <WizardStateId>1</WizardStateId>
  <CreateDate>2004-08-03T00:00:00.0000000-04:00</CreateDate>
  <TotalAmount>245.12</TotalAmount>
  <PaymentRejected>True</PaymentRejected>
  <CheckPay>false</CheckPay>
  </ShoppingCartItem>
</dsShoppingCartItem>
 
I've tried every possible permutation that I can think of on the XSLT sheet I posted a few minutes ago - all I want to do is match all of the locations and put the DBAName in a Table.  Actually I want to do a hell of a lot more but that's the starting point and I'm getting NOWHERE.  Is there something about the XML control I'm missing?  Nothing like spending 2 hours on a Sunday trying to get one stinking match to work and coming up with Zilch. 
 
Filed under:

Comments

# William said on November 14, 2004 5:12 PM:

an obvious problem is the XML namespaces dont match.

declare a namespace in XSLT to match the one in XML, something like this:
xmlns:xsd="http://tempuri.org/dsShoppingCartItem.xsd"

then when you are referring to an element in XSLT, use that prefix:
xsd:Location

... and i like XSLT. its basically voodoo magic for XML

# William said on November 14, 2004 5:16 PM:

I'll like it to when I get the hang of it - but it's busting my b4lls right now. Let me give that a try - I definitely appreciate it !

# William said on November 14, 2004 5:52 PM:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://tempuri.org/dsShoppingCartItem.xsd" version="1.0">

<xsl:template match="xsd:dsShoppingCartItem">
Bill
<xsl:apply-templates select="xsd:Location"/>
</xsl:template>

<xsl:template match="xsd:Location">
<table width="100%" border="1">
<tr>
<td>
<b><xsl:value-of select="xsd:DBAName" /> </b>
</td>
</tr>
</table>
</xsl:template>

</xsl:stylesheet>
<!--?xml-stylesheet type="text/xsl" href="bill.xslt"?-->

# William said on November 14, 2004 5:57 PM:

The one thing I finally figured out is that the dsShoppingCartItem was causing the part of the problem. I added this :<?xml version="1.0" encoding="ISO-8859-1" ?> to the top of the document and at first, nothing happened - then I realized I left a space to the left of the declaration.

So this works perfectly for instance:

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

<xsl:template match="/">
<html>
<body>
<h2>This is killing me</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">DBAName</th>
<th align="left">Location</th>
</tr>
<xsl:for-each select="dsShoppingCartItem/Location">
<tr>
<td><xsl:value-of select="DBAName"/></td>
<td><xsl:value-of select="TotalAmount"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Which is slightly different than my original post - but it was the traversal that caused at least one problem. The thing that's weird w/ the XML Control is that if you change the root node and it doesn't find it, then it will simply output all of the text - so it looks like it's working but just finding the wrong stuff.

When I did this with code instead of the control it made a lot more sense - not because the control is screwy but because I'm a bubble head.

I will get this - XSLT looks way too cool to be lame at it.

# William said on November 14, 2004 5:58 PM:

Casey: You kick a33! I sort of fixed part of the problem but I just took your code and it works like a charm - You are the MAN!

# William said on November 14, 2004 6:00 PM:

Let me restate that Casey - you are the man - that makes EVERYTHING so much easier that I'll actually get to go home this evening ;-)

# William said on November 14, 2004 7:34 PM:

In addition you can find a few good examples here:
http://www.w3.org/TR/xslt#section-Document-Example


XSL rocks if you have something that implements it. It is basically "Parse this in the following way" which for anyone who has ever had to process entire XML documents ( just to see if the data you wanted is even in the doc and then base your logic on what you find ) XSL is a blessing.


*begin rant*
I have a 6kb XML parser for small devices using J2ME but it doesn't support XSL. Yet another place where Sun has really dropped the ball on small device work.

Why the f#ck can't Research in Motion support C++ or C# ? I could save so much time and effort over using J2ME. They say they have a C++ API but there is zero documentation for it. I mean none, not even any docs explaining how to upload a C++ app to the device or what compiler they support, or how to find docs on getting a compiler set up to target their OS. I know it's not windows CE so WTF is it? They don't say. So I'm stuck with J2ME and a f#ck load of XML parsing vice being able to distill docs down using XSL.
*end rant*

# William said on November 14, 2004 8:40 PM:

glad it worked Bill.
i made that mistake no less than 3 times when learning XSLT.
then it took me about 6 more months before i really understood it.
now it looks like XQuery is the chosen one.
i've read a couple books on it but haven't actually worked with it yet

also, your 2nd XSLT post switched to using an xsl:for-each.
i consider that easier to understand than apply-templates,
because you can sort of think of it procedurally

# William said on November 14, 2004 9:07 PM:

Thanks KC. I was just looking through examples trying to find zomething that worked. I wasn't going to get there b/c that root note wasn't getting handled correctly Once you showed me that it 'clicked' and yes, the foreach seems to make a lot more sense and being able to navigate easily, which I can do now kicks a33. I can just imagine the possibilities with this stuff - I wished I'd have bit the bullet a while ago but it will be fun to learn.

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications