2

I'm trying to extract the headline from the below XML from the Met Office web service using XSLT, however my 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 and frosty start in west, but cloudier in Kent with isolated showers. Then dry with sunny periods. Increasing cloud in west later will bring coastal showers with freshening southerly winds. Chilly inland, but 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 the root to /RegionalFcst and attempted other similar changes, such as adding a leading slash before FcstPeriods, but nothing works until I remove the first and last line from the source XML - then it works perfectly.

This is fine in testing, but of course I want to use the web service provided by Met Office and that's how they present it.

Any ideas?

hazymat
  • 359
  • 3
  • 18

1 Answers1

11

The problem: your XML puts its elements in a namespace.

Solution: declare the same namespace in your stylesheet, assign it a prefix and use that prefix to address the elements in the 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>
michael.hor257k
  • 96,733
  • 5
  • 30
  • 46
  • Follow-up question. I need to remove html tags from the response as I'm not processing for web. When I remove , , the response I get is _Frosty start. Bright or sunny day._ I need this response without the tag. Why would I be getting this and how do I remove it? Thanks for your help. – hazymat Jan 13 '16 at 13:30
  • @hazymat Could you edit your question and post the **exact** result you want to get (as code)? – michael.hor257k Jan 13 '16 at 13:35
  • Please ignore my last comment, I had forgotten I needed to include another line to properly encode the output for the system I was using. So it's working as intended. Thanks again for your help. – hazymat Jan 13 '16 at 13:42
  • @michael.hor257k thx , even I was facing the same issue and as per ur suggestion it worked. – sreevathsa a Oct 28 '16 at 05:28
  • Thanks this helped solving my problem, too. It should be emphasized that this only works for the default namespace "xmlns=...". If there are any other qualified namespaces in the source xml file they have to be added to "xsl:stylesheet", used in the xpath (without the temporary prefix for the default namespace), and added to "exclude-result-prefixes" (as space separated list). – Stefan Dolezel Jun 01 '17 at 09:10
  • @StefanDolezel No, this works for **any** namespaces used by the source XML. – michael.hor257k Jun 01 '17 at 09:28
  • Yes, it works the same way. I meant that only for the default namespace a prefix has to be _created_ ("met" in the example above). If the source file already has a qualified namespace, e.g. "xmlns:myns=...", one may just use this prefix in the xslt (or relabel it if it is more convenient). – Stefan Dolezel Jun 01 '17 at 12:05
  • Is there any alternative to this solution. The system I am working with doesn't support the extensions – Amir Oct 29 '20 at 13:18
  • @Amir What extensions? This is pure XPath/XSLT. – michael.hor257k Oct 29 '20 at 15:27
  • Sorry I was wrong, the error was comming from the setting in the version setting. By mistake i was using version 1 and since this is code is written for version 2 i was getting error. My bad! Your solution works perfectly. – Amir Nov 22 '20 at 11:59
  • @Amir Do note that the above is an XSLT 1.0 solution. If you're using XSLT 2.0 or higher, you can take advantage of `xpath-default-namespace`. – michael.hor257k Nov 22 '20 at 12:27
  • well funny because it didnt work till I changed the version to 2. Thanks, tomorrow I will try this solution. – Amir Nov 22 '20 at 13:04