xml - Adding Attribute to Root Element of Output -


so need add 4 attributes root element (<weather>) of xml output creating using xslt. 4 elements details of weather station data obtained from. contained within stations.xml file , has structure follows:

<stations>    <station>        <site>81123</site>        <name>bendigo airport</name>        <latitude>36.74</latitude>        <longitude>144.33</longitude>        <state>vic</state>    </station>    <station>        <site>81124</site>        <name>yarrawonga</name>        <latitude>36.03</latitude>        <longitude>146.03</longitude>        <state>vic</state>    </station> </stations> 

an xpath tester tells me following correct predicate obtain required station node.

./stations/station[site=81123] 

i've tried matching think i've lost way here. if can offer assistance great. code have far below:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output method="xml" indent="yes"/>  <!-- select <measurement> elements of various input files --> <xsl:variable name="maxtemp" select="document('max_temp.xml')/*" /> <xsl:variable name="rainfall" select="document('rainfall.xml')/*" /> <xsl:variable name="solar" select="document('solar.xml')/*" /> <xsl:variable name="mintemp" select="document('min_temp.xml')/*" /> <xsl:variable name="stations" select="document('station.xml')/*" />  <!-- index <measurement> elements station , date --> <xsl:key name="kmeasurement" match="measurement"     use="concat(day, '/', month, '/', year)" />  <xsl:template match="/">     <weather>         <xsl:apply-templates select="weather" />         <xsl:apply-templates select="$maxtemp/measurement" />     </weather> </xsl:template>  <xsl:template match="weather">     <xsl:attribute name="stationid"><xsl:value-of select="$stations/station[site=81123]/site" /></xsl:attribute>     <xsl:attribute name="stationname"><xsl:value-of select="$stations/station[site=81123]/name" /></xsl:attribute>     <xsl:attribute name="latitude"><xsl:value-of select="$stations/station[site=81123]/latitude" /></xsl:attribute>     <xsl:attribute name="longitude"><xsl:value-of select="$stations/station[site=81123]/longitude" /></xsl:attribute> </xsl:template>  <xsl:template match="measurement">     <xsl:variable name="currentkey" select="concat(day, '/', month, '/', year)" />      <measurement>         <date><xsl:value-of select="concat(day, '/', month, '/', year)" /></date>          <!-- since handling maxtemp measurements here, can output directly -->         <maxtemp><xsl:value-of select="maxtemp"/></maxtemp>          <!-- access others need context switch , key lookup -->         <xsl:for-each select="$rainfall">             <rainfall><xsl:value-of select="key('kmeasurement', $currentkey)/volume" /></rainfall>         </xsl:for-each>         <xsl:for-each select="$solar">             <solar><xsl:value-of select="key('kmeasurement', $currentkey)/dailysolarexposure" /></solar>         </xsl:for-each>         <xsl:for-each select="$mintemp">             <mintemp><xsl:value-of select="key('kmeasurement', $currentkey)/mintemp" /></mintemp>         </xsl:for-each>     </measurement> </xsl:template> 

to minimize example problem @ hand, following stylesheet:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:variable name="stations" select="document('station.xml')/*" />  <xsl:template match="/">     <weather>         <xsl:variable name="station" select="$stations/station[site=81123]" />         <xsl:attribute name="stationid"><xsl:value-of select="$station/site" /></xsl:attribute>         <xsl:attribute name="stationname"><xsl:value-of select="$station/name" /></xsl:attribute>         <xsl:attribute name="latitude"><xsl:value-of select="$station/latitude" /></xsl:attribute>         <xsl:attribute name="longitude"><xsl:value-of select="$station/longitude" /></xsl:attribute>          <!-- create content here -->      </weather> </xsl:template> 

will return:

<?xml version="1.0" encoding="utf-8"?> <weather stationid="81123" stationname="bendigo airport" latitude="36.74" longitude="144.33"/> 

provided it's run against valid xml input , document named station.xml in same directory stylesheet located.


if prefer, can shorten above code to:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:variable name="stations" select="document('station.xml')/*" />  <xsl:template match="/">     <xsl:variable name="station" select="$stations/station[site=81123]" />     <weather stationid="{$station/site}" stationname="{$station/name}" latitude="{$station/latitude}" longitude="{$station/longitude}">          <!-- create content here -->      </weather> </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -