【问题标题】:Get rid of XML namespaces from HTML generated by XSL in PHP从 PHP 中 XSL 生成的 HTML 中去除 XML 命名空间
【发布时间】:2017-04-03 19:14:31
【问题描述】:

我正在使用以下 PHP 例程将使用 XSL 的 XML 转换为 HTML。问题是在转换期间 xmlns 命名空间作为属性插入到 h2 元素中。

PHP:

$url = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";

$xml_doc = file_get_contents($url);
$xml = simplexml_load_string($xml_doc);

$xsl_doc = new DOMDocument();
$xsl_doc->load("currency_transform.xsl");

$xsl = new XSLTProcessor();
$xsl->ImportStyleSheet($xsl_doc);
echo $xsl->transformToXML($xml);

XML(来自上面的链接):

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
    <gesmes:subject>Reference rates</gesmes:subject>
    <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time='2017-04-03'>
            <Cube currency='USD' rate='1.0661'/>
            <Cube currency='JPY' rate='118.64'/>
            <Cube currency='BGN' rate='1.9558'/>
            <Cube currency='CZK' rate='27.044'/>
            <Cube currency='DKK' rate='7.4374'/>
            <Cube currency='GBP' rate='0.85260'/>
            <Cube currency='HUF' rate='308.68'/>
            <Cube currency='PLN' rate='4.2279'/>
            <Cube currency='RON' rate='4.5495'/>
            <Cube currency='SEK' rate='9.5145'/>
            <Cube currency='CHF' rate='1.0682'/>
            <Cube currency='NOK' rate='9.1468'/>
            <Cube currency='HRK' rate='7.4305'/>
            <Cube currency='RUB' rate='60.0802'/>
            <Cube currency='TRY' rate='3.8831'/>
            <Cube currency='AUD' rate='1.4012'/>
            <Cube currency='BRL' rate='3.3314'/>
            <Cube currency='CAD' rate='1.4229'/>
            <Cube currency='CNY' rate='7.3423'/>
            <Cube currency='HKD' rate='8.2858'/>
            <Cube currency='IDR' rate='14203.12'/>
            <Cube currency='ILS' rate='3.8690'/>
            <Cube currency='INR' rate='69.2805'/>
            <Cube currency='KRW' rate='1191.09'/>
            <Cube currency='MXN' rate='19.9794'/>
            <Cube currency='MYR' rate='4.7202'/>
            <Cube currency='NZD' rate='1.5226'/>
            <Cube currency='PHP' rate='53.456'/>
            <Cube currency='SGD' rate='1.4899'/>
            <Cube currency='THB' rate='36.658'/>
            <Cube currency='ZAR' rate='14.4506'/>
        </Cube>
    </Cube>
</gesmes:Envelope>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
  xmlns:eurofxref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
    <xsl:output method = "html"/>
<xsl:template match="/">

<html>
<body>
  <h2>Kursy walut</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Waluta</th>
      <th style="text-align:left">Kurs</th>
    </tr>
    <xsl:for-each select="gesmes:Envelope/eurofxref:Cube/eurofxref:Cube/eurofxref:Cube">
    <tr>
      <td><xsl:value-of select="@currency"/></td>
      <td><xsl:value-of select="@rate"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

生成的 HTML 代码:

<div class="currency_table">
<h2 xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns:eurofxref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">Kursy walut</h2><table xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns:eurofxref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref" border="1">
<tbody><tr bgcolor="#9acd32">
<th style="text-align:left">Waluta</th>
<th style="text-align:left">Kurs</th>
</tr>
<tr>
<td>USD</td>
<td>1.0661</td>
</tr>

...

<td>ZAR</td>
<td>14.4506</td>
</tr>
</tbody></table>
</div>

我的问题是:如何从 h2 元素中删除 xmlns 属性?为什么它还在那里?

【问题讨论】:

标签: php html xml xslt


【解决方案1】:

如何去除 h2 元素中的 xmlns 属性?

尝试改变这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
  xmlns:eurofxref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">

到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
  xmlns:eurofxref="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"
  exclude-result-prefixes="gesmes eurofxref">

未经测试,因为没有提供输入示例。顺便说一句,命名空间声明不是属性。


为什么它还在那里?

因为显然您的输入节点位于命名空间中,并且您需要这些前缀来解决它们 - 请参阅:XSLT Transform doesn't work until I remove root node

【讨论】:

  • @KrisOye 不,不是。 XSL 转换的输入是 XML 文档。
  • 嗯,源 URL 已包含在内,这对我来说已经足够好了。我提交了修改,但我猜有人已经这样做了。
【解决方案2】:

您可以从 XSL 样式表中删除命名空间并忽略 for-each 选择中的命名空间,以避免在输出中出现命名空间,例如:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "html"/>

<xsl:template match="/">
    <html>
    <body>
      <h2>Kursy walut</h2>
      <table border="1" >
        <tr bgcolor="#9acd32">
          <th style="text-align:left">Waluta</th>
          <th style="text-align:left">Kurs</th>
        </tr>
        <xsl:for-each select="//*[local-name()='Cube']/*[local-name()='Cube'][@currency]">
        <tr>
          <td><xsl:value-of select="@currency"/></td>
          <td><xsl:value-of select="@rate"/></td>
        </tr>
        </xsl:for-each>
      </table>
    </body>
    </html>
</xsl:template>
</xsl:stylesheet>

然后输出如下:

<html><body>
<h2>Kursy walut</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Waluta</th>
<th style="text-align:left">Kurs</th>
</tr>
<tr>
<td>USD</td>
<td>1.0661</td>
</tr>
<tr>
<td>JPY</td>
<td>118.64</td>
</tr>
<tr>
<td>BGN</td>
<td>1.9558</td>
</tr>
<tr>
<td>CZK</td>
<td>27.044</td>
</tr>
<tr>
<td>DKK</td>
<td>7.4374</td>
</tr>

【讨论】:

  • 谢谢! :D 帮助很大,尤其是 XPath。
  • 添加了一个额外的 [@currency] 以避免出现空白行。
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 2013-03-28
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多