how to extract from _inside_ xml tag

E

edfialk

here's my xml:

<dimension name="lat" units="degrees_north"
dim_type="index">
<min>-90</min>
<max>90</max>
<count>181</count>
</dimension>
<dimension name="lon" units="degrees_east"
dim_type="index">
<min>-180</min>
<max>179</max>
<count>360</count>
</dimension>


I'm having difficulty finding out which of these tags are name="lat"
and which are name="lon".
right now I have:
<xsl:for-each select="/dataset/LatLonTimeCube/dimensions/
dimension">
<xsl:if test="name=lon">
&lt;westbc&gt;<xsl:value-of select="min"/>&lt;/westbc&gt;
&lt;eastbc&gt;<xsl:value-of select="max"/>&lt;/eastbc&gt;
</xsl:if>
<xsl:if test="name=lat">
&lt;northbc&gt;90&lt;/northbc&gt;
&lt;southbc&gt;-90&lt;/southbc&gt;
</xsl:if>
</xsl:for-each>

I know the 90 in there is a constant, I was just testing.

For the above xml, I need to print out:

west: -179
north: 90
east: 180
south: -90

BUT, my if test="name=lon" is always failing. Does anyone know how to
test the value of name from inside the dimension tag?

I would extremely appreciate any help.
Thanks!
-Ed
 
J

Joseph Kesselman

<xsl:if test="name=lon">

That compares the value of the <name> child element against the value of
the <lon> child element -- which of course doesn't exist. Change it to

<xsl:if test="name='lon'">
 
E

edfialk

Hey Joe thanks, but it still doesn't seem to be passing the test.

<xsl:if test="name='lon'">
&lt;westbc&gt;<xsl:value-of select="min"/>&lt;/westbc&gt;
&lt;eastbc&gt;<xsl:value-of select="max"/>&lt;/eastbc&gt;
</xsl:if>

<xsl:if test="name='lat'">
&lt;northbc&gt;90&lt;/northbc&gt;
&lt;southbc&gt;-90&lt;/southbc&gt;
</xsl:if>

doesn't output anything because it's still failing the test.
If I change the ifs to a choose/otherwise, the otherwise is getting
printed, because it's failing the test.

Any other ideas?
 
J

Joseph Kesselman

By the way,
&lt;westbc&gt;<xsl:value-of select="min"/>&lt;/westbc&gt;

is *****EXTREMELY***** bad practice. If you want to construct XML
output, use literal XML or the appropriate XSLT directives.

<westbc><xsl:value-of select="min"/></westbc>
or
<xsl:element name="westbc"><xsl:value-of select="min"/></xsl:element>


As to why things aren't matching: I'll take another look, but I suspect
you've given us an incomplete description of your problem.
 
E

edfialk

Ya, I've never used XSL before, my co-worker has come to me with this
problem hoping I can help. I'll make her change her output structure.

As for my problem, I'm not sure what else to include. i can't attach
files, but basically I've got:

<dimension name="lat" units="degrees_north"
dim_type="index">
<min>-90</min>
<max>90</max>
<count>181</count>
</dimension>
<dimension name="lon" units="degrees_east"
dim_type="index">
<min>-180</min>
<max>179</max>
<count>360</count>
</dimension>

and:

<xsl:for-each select="/dataset/LatLonTimeCube/dimensions/dimension">
<xsl:choose>
<xsl:when test='name="lat"'>
SUCCESS!
</xsl:when>
<xsl:eek:therwise>
failed
</xsl:eek:therwise>
</xsl:choose>

should output one SUCCESS and one failed, but it's always failed.
I would be happy to host these 2 files on my server so anyone could
take a look at them if they wish.
 
J

Joseph Kesselman

edfialk said:
As for my problem, I'm not sure what else to include.

Including a minimal runnable example is usually a good idea -- a
complete input file, and a complete stylesheet, both trimmed down to
absolute essentials needed to demonstrate the problem.
I would be happy to host these 2 files on my server so anyone could
take a look at them if they wish.

If they're short enough that we don't get bogged down in irrelevant
detail, that will do the job. If not, trim 'em down first, them post.

(Among other things, the process of trying to reduce the problem to its
simplest form is often enough by itself to expose the mistake.)
 
J

Johannes Koch

Joseph said:
That compares the value of the <name> child element against the value of
the <lon> child element -- which of course doesn't exist. Change it to

<xsl:if test="name='lon'">

As name is an attribute, it must be "@name='lon'" instead.
 
J

Joseph Kesselman

As name is an attribute, it must be "@name='lon'" instead.

Blush. Can't believe I missed that typo...
 

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
474,008
Messages
2,570,269
Members
46,871
Latest member
Stephendes

Latest Threads

Top