XSLT 转换在我删除根节点之前不起作用 [英] XSLT Transform doesn't work until I remove root node

查看:21
本文介绍了XSLT 转换在我删除根节点之前不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XSLT 从 Met Office Web 服务的以下 XML 中提取标题,但是我的 XSLT 选择返回空白.

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.

来源:

<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>

我的 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>

我已将根目录更改为 /RegionalFcst 并尝试了其他类似的更改,例如在 FcstPeriods 之前添加前导斜杠,但在我从源 XML 中删除第一行和最后一行之前没有任何效果 -然后它完美地工作.

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.

这在测试中没问题,但当然我想使用 Met Office 提供的网络服务,这就是他们提供的方式.

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.

有什么想法吗?

推荐答案

问题:您的 XML 将其元素放在命名空间中.

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

解决方案:在样式表中声明相同的命名空间,为其分配一个前缀并使用该前缀来寻址源 XML 中的元素:

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>

这篇关于XSLT 转换在我删除根节点之前不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆