RE: [xsl] please help. newbie needs help!!

Subject: RE: [xsl] please help. newbie needs help!!
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Mon, 10 May 2004 22:58:37 -0600
Understanding XPath goes beyond telling you how to get "THE WATHERSHED
GROUP, LLC" from your XML data.  There are many answers to this question
and it depends on various factors.  For example, will "THE WATHERSHED
GROUP, LLC" always be in the "Row" where ss:Index="2" or is it possible
it could be somewhere else.  

I do understand your frustration.  I was once new to the XSLT/XPath
world and although it didn't necessarily help me understand the
technology better sometimes the answer was what I needed first and a
lesson on how it works after.  As such I will give you the answer and
then a URI to where you can study up on XPath so that you can eliminate
some of your future frustrations by learning why this:

<xsl:value-of
select="Workbook/Worksheet/Table/Row[ss:Index="2"]/Cell[ss:Index="1"]/Da
ta >

Will output:

THE WATHERSHED GROUP, LLC

Into your data stream coming from the XSLT processor.

Keep in mind that the above example starts from the wherever your XML
parser happens to be at the present moment.  So if you have created a
simple template and matched it to the root as such:

<xsl:template match="/">
	<xsl:value-of
select="Workbook/Worksheet/Table/Row[ss:Index="2"]/Cell[ss:Index="1"]/Da
ta >
</xsl:template>

Then you will get the proper result.  But if your match is set to
something deeper into your data such as "Workbook/Worksheet" the above
XPath will evaluate to nothing and therefore return nothing.  You can
change your context within your XML set various ways but the most common
way for someone first starting off in XSLT and XPath is within a
for-each loop.  If you were to have a for-each loop whose select
attribute was equal to "Workbook/Worksheet/Table/Row" and then try to
get the value of the Cell containing the company name using the XPath
from above it wouldn't work.

To better understand XSLT and XPath and not have to plow through a huge
number of pages that make little or no sense to the first time XSLT
developer I would highly recommend Dave Pawson's XSL FAQ.  It's a great
starting point to both answer the basic questions while give you
resources to find answers to the more difficult ones.  You can find it
here:

http://www.dpawson.co.uk/xsl/index.html

Don't feel like you can't use this list as a resource.  Although I tend
to appear and disappear depending on my current workload there are a lot
of really great people in here who really know there stuff and find it a
pleasure to help those who need help getting things started or working
out difficult problems that can't be found in any of the references or
tutorials (well, they can be found, just not very easily to someone
unfamiliar with the technology).  I can advise you that doing a bit of
leg work on your own by first going to the archives of this list or
searching places like Dave Pawson's site or Jeni Tennison's site (
http://www.jenitennison.com/xslt/index.html ) will gain you a lot more
notice simply because it's encouraging to us to see that someone is
doing all they can to understand the technology before turning to
someone to just give them the answer.

Another recommendation if you know you will be doing a lot of work in
XSLT in the future.  Go to Amazon or whatever you prefer as far as
purchasing your tech books and find a copy of Michael Kay's XSLT
reference.  Here's the Amazon link...
http://www.amazon.com/exec/obidos/tg/detail/-/0764543814/qid=1084250811/
sr=1-1/ref=sr_1_1/002-6621421-6316854?v=glance&s=books 

He also has a 2.0 reference coming out although at the moment 2.0 has
not reached final recommendation and as such only has one XSLT processor
that supports it (Saxon, Michael Kay's XSLT processor.) So I would
probably avoid getting yourself confused with the difference between the
two until such time as your processors vendor releases support for the
2.0 recommendation (probably around the same time the W3C releases the
current draft, or a slighly modified version of it, as a final
recommendation.)  I tell you this because I would hate for you to
purchase (actually, you can only pre-purchase it, its hasn't been
officially released yet) the 2.0 reference thinking it was the latest
and greatest only to find out that your processor only supports 1.0.

I wish you the best of luck and encourage you to keep at it as far as
learning XSLT and XPath goes.  Like any other language it can be
difficult to learn at first but once you get it you get it and you'll be
happy you spent the time learning it as it is a VERY powerful tool.

Best regards,

<M:D/>

-----Original Message-----
From: Jeremy Allen [mailto:jallen@xxxxxxxxxxxxxx] 
Sent: Monday, May 10, 2004 9:51 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] please help. newbie needs help!!

I need an xpath expression for getting data from this xml, for example
what
expression would I use to get the string "THE WATERSHED GROUP, LLC"? Any
help appreciated!!!

EXALMPLE:
<?xml version="1.0"?>
<Workbook
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40";>
  <DocumentProperties
    xmlns="urn:schemas-microsoft-com:office:office">
    <Author>PC Entry</Author>
    <LastAuthor>PC Entry</LastAuthor>
    <Created>2004-05-03T09:02:51Z</Created>
  </DocumentProperties>
  <OfficeDocumentSettings />
  <ExcelWorkbook
    xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>7815</WindowHeight>
    <WindowWidth>7500</WindowWidth>
    <WindowTopX>360</WindowTopX>
    <WindowTopY>45</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
    </Style>
    <Style ss:ID="s21">
      <NumberFormat ss:Format="Short Date" />
    </Style>
    <Style ss:ID="s22">
      <NumberFormat ss:Format="Fixed" />
    </Style>
  </Styles>  <Worksheet ss:Name="Sheet1">
    <Table>
      <Column ss:Index="1" ss:Width="315"/>
      <Column ss:Index="2" ss:Width="153"/>
      <Column ss:Index="3" ss:Width="297"/>
      <Column ss:Index="4" ss:Width="126"/>
      <Column ss:Index="5" ss:Width="126"/>
      <Column ss:Index="6" ss:Width="72"/>
      <Column ss:Index="7" ss:Width="90"/>
      <Column ss:Index="8" ss:Width="108"/>
      <Row ss:Index="1">
        <Cell ss:Index="1" >
          <Data ss:Type="String">111-55555</Data>
        </Cell>
      </Row>
      <Row ss:Index="2">
        <Cell ss:Index="1" >
          <Data ss:Type="String">THE WATERSHED GROUP, LLC</Data>
        </Cell>
      </Row>
      <Row ss:Index="3">
        <Cell ss:Index="1" >
          <Data ss:Type="String">GUS'S WATERSHED</Data>
        </Cell>
      </Row>

Current Thread