simple XSLT question

C

Chrism2671

I'm new to XSLT/XML and I have a very simple, quick question. i've
been trying to convert simple xml files into CSV files and have made a
simple XSLT template using the w3 tutorials, but it doesn't seem to
display anything. It does display plain text I enter into the
templates, the value-of tags just render whitespace.

If anybody can write a template of just a few lines just to
demonstrate how to get it to display something from this XML I would
be very very very appreciative.

<OrganisationList xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="nhs.uk">
<Organisation OrganisationType="15" ParentOrganisation="5HX"
OrganisationCode="E85053">
<PremisesList>
<Premises SiteOrder="0" mainsite="true" PremisesCode="UB5
4AT^1">
<ContactDetails>
<TextPhone>none</TextPhone>
<Fax>020 8422 8040</Fax>
<Telephone>020 8864 8133</Telephone>
</ContactDetails>
<Address>
<Line1>45 Doncaster Drive</Line1>
<Line2>Northolt</Line2>
<Line3>Middx</Line3>
<Line4>none</Line4>
<Line5>none</Line5>
<PostCode>UB5 4AT</PostCode>
</Address>
<PremisesNameList>
<PremisesName NameType="">
<TheName>Dr Balachandran, G</TheName>
</PremisesName>
</PremisesNameList>
</Premises>
</PremisesList>
<NameList OrganisationCode="E85053">
<Name>
<TheName>Dr Balachandran, G</TheName>
</Name>
</NameList>
</Organisation>
</OrganisationList>
 
M

Martin Honnen

Chrism2671 said:
I'm new to XSLT/XML and I have a very simple, quick question. i've
been trying to convert simple xml files into CSV files and have made a
simple XSLT template using the w3 tutorials, but it doesn't seem to
display anything. It does display plain text I enter into the
templates, the value-of tags just render whitespace.

If anybody can write a template of just a few lines just to
demonstrate how to get it to display something from this XML I would
be very very very appreciative.

<OrganisationList xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="nhs.uk">
^^^^^^^^^^^^^^
With that default namespace declaration your stylesheet needs to declare
that namespace too but bind a prefix to it so that your patterns and
XPath expressions match/select elements in that namespace:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:nhs="nhs.uk">

<xsl:eek:utput method="text"/>

<xsl:template match="/">
<xsl:value-of
select="nhs:OrganisationList/nhs:Organisation/nhs:premisesList/nhs:premises/nhs:ContactDetails/nhs:Fax"/>
</xsl:template>

</xsl:stylesheet>
 
C

Chrism2671

Thanks Martin that's brilliant; I would have never figured that
myself. I've gotten as far as the code below, but it doesn't seem to
like my for-each params, which I have based upon your example. It
doesn't output anything, but doesn't through an error either. Any
ideas what I've done wrong?

Thanks once again,
Chris.

<?xml version='1.0'?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:nhs="nhs.uk">

<xsl:template match="/">
<xsl:for-each select="nhs:OrganisationList/nhs:Organisation">
<xsl:value-of select="nhs:OrganisationList/nhs:Organisation/
nhs:premisesList/nhs:premises/nhs:ContactDetails/nhs:Fax"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="OrganisationList">

</xsl:template>
</xsl:stylesheet>
 
M

Martin Honnen

Chrism2671 said:
<xsl:template match="/">
<xsl:for-each select="nhs:OrganisationList/nhs:Organisation">
<xsl:value-of select="nhs:OrganisationList/nhs:Organisation/
nhs:premisesList/nhs:premises/nhs:ContactDetails/nhs:Fax"/>

Inside of the xsl:for-each you need to use XPath expressions relative to
the node the for-each selects. So once you have the for-each select an
nhs:Organisation element you need e.g.
<xsl:value-of
select="nhs:premisesList/nhs:premises/nhs:ContactDetails/nhs:Fax"/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top