Jump to content

Extract XML attributes


edmundian

Recommended Posts

HI ,I know this type of question might have been answered many times but here I am desprately want some help with XSLT.In my case, I have XML data that looks like :<road_traffic_message message_id="1333854" message_generation_time="2006-11-28T02:34:30+0" version_number="2" severity_factor="severe" ><WGS84 latitude="55.91857" longitude="-4.405708" />I have to write some XSL that will recognise both lines, ignore the rest but simply grab message_generation_time, severity, latitude and longitude from different elements into a table. What I have tried was:<xsl:template match="//road_traffic_message"> <tr> <td> <xsl:value-of select="@message_generation_time"/> </td> <td> <xsl:value-of select="@severity_factor"/> </td> <xsl:for-each select="//WGS84> <xsl:if test="@latitude !=''"> <td> <xsl:value-of select="@latitude"/> </td> </xsl:if> <xsl:if test="@longitude !=''"> <td> <xsl:value-of select="@longitude"/> </td> </xsl:if> </xsl:for-each> </tr> Hoping to extract those information and display on the table, but nothing has happened in the intended way.Any ideas what I have done it wrong?? Thank your for reading this

Link to comment
Share on other sites

It all depends on the possible structures of the complete XML file. And what exactly is your desired output? Is the WGS84 element present when road_traffic_message is not or vise versa? Are they always on the same level? Are they grouped by pairs in any matter?

Link to comment
Share on other sites

It all depends on the possible structures of the complete XML file. And what exactly is your desired output? Is the WGS84 element present when road_traffic_message is not or vise versa? Are they always on the same level? Are they grouped by pairs in any matter?
I am intended to display a table of 4 columns containing all those information. The WGS84 element present when road_traffic_message is and they arent on the same level , here it's how it looks like:<road_traffic_message message_id="1333854" message_generation_time="2006-11-28T02:34:30+0" version_number="2" start_time="2006-11-28T02:04:28+0" stop_time="2006-11-28T03:34:30+0" severity_factor="severe"> <location_container language="English"> <location_coordinates location_type="route"> <WGS84 latitude="55.91857" longitude="-4.405708"/> </location_coordinates> </location_container></road_traffic_message> If there is anything not yet clarify please let me know .Many thanks
Link to comment
Share on other sites

Perhaps something like this:

<xsl:template match="//road_traffic_message"><tr><td> <xsl:value-of select="@message_generation_time"/> </td> <td> <xsl:value-of select="@severity_factor"/> </td> <xsl:for-each select=".//WGS84/@*[. !='']"><td><xsl:value-of select="."/></td></xsl:for-each></tr>

Link to comment
Share on other sites

Perhaps something like this:
<xsl:template match="//road_traffic_message"><tr><td> <xsl:value-of select="@message_generation_time"/> </td> <td> <xsl:value-of select="@severity_factor"/> </td> <xsl:for-each select=".//WGS84/@*[. !='']"><td><xsl:value-of select="."/></td></xsl:for-each></tr>

Thanks for your kind reply,I really appreciated it. I've just tried it on my PC, but something strange happened is that instead of 4 columns as I desired , it came out nowhere forming 6 columns , duplicating the same infomation as from WGS84 elements. Any ideas why this might be ? This is what it looks like now :Message Generated Time| Severity| Latitude | Longtitude | -----------------------------------------------------------------------------------------------------2006-11-28T02:34:30+0 | severe | 55.91857 | -4.405708 |2006-11-28T01:14:07+0 | severe | 50.614929| -2.456895 | 50.61437 -2.4528192006-11-28T01:14:16+0 | medium | 57.592004| -6.358475 |2006-11-28T02:34:41+0 | medium | 51.59861 | -3.786362 | 51.662265 -3.8781382006-11-28T02:34:38+0 | medium | 51.601849| -2.925224 | 51.56672 -3.0357942006-11-28T01:14:19+0 | medium | 53.221017| -4.188662 |
Link to comment
Share on other sites

Odd... unless there were two WGS84 elements per road_traffic_message element. Isn't it always one?If there are more, but you always want the first one, try pointing the position in the for-each. That is:

<xsl:for-each select=".//WGS84[1]/@*[. !='']">

Link to comment
Share on other sites

Odd... unless there were two WGS84 elements per road_traffic_message element. Isn't it always one?If there are more, but you always want the first one, try pointing the position in the for-each. That is:
<xsl:for-each select=".//WGS84[1]/@*[. !='']">

oh my god ..it magically work ... thank you so much ..just the tiny little thing that i want to ask u. How does this [.!= ] work ?thanks
Link to comment
Share on other sites

Huh. I wasn't really sure it will. Good! :) The predicate[. !='']works by selecting the current node (this is what the dot is for) and returning true if that node has a content, different from (the "!=") an empty string (the apostrophes).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...