xml - XSLT Transform doesn't work until I remove root node -
i'm trying extract headline below xml met office web service using xslt, xslt select returns blank.
source:
<regionalfcst xmlns="www.metoffice.gov.uk/xml/metoregionalfcst" createdon="2016-01-13t02:14:39" issuedat="2016-01-13t04:00:00" regionid="se"> <fcstperiods> <period id="day1to2"> <paragraph title="headline:">frosty start. bright or sunny day.</paragraph> <paragraph title="today:">a clear , frosty start in west, cloudier in kent isolated showers. dry sunny periods. increasing cloud in west later bring coastal showers freshening southerly winds. chilly inland, less cold near coasts. maximum temperature 8c.</paragraph> </period> </fcstperiods> </regionalfcst>
my xslt:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <html> <body> <xsl:value-of select="fcstperiods/period/paragraph"/> </body> </html> </xsl:template> </xsl:stylesheet>
i've changed root /regionalfcst , attempted other similar changes, such adding leading slash before fcstperiods, nothing works until remove first , last line source xml - works perfectly.
this fine in testing, of course want use web service provided met office , that's how present it.
any ideas?
the problem: xml puts elements in namespace.
solution: declare same namespace in stylesheet, assign prefix , use prefix address elements in source xml:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:met="www.metoffice.gov.uk/xml/metoregionalfcst" exclude-result-prefixes="met"> <xsl:template match="/"> <html> <body> <xsl:value-of select="met:regionalfcst/met:fcstperiods/met:period/met:paragraph[@title='headline:']"/> </body> </html> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment