从PHP中的XSL生成的HTML中删除XML命名空间

时间:2017-04-03 19:14:31

标签: php html xml xslt

我正在使用以下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属性?它为什么在那里?

2 个答案:

答案 0 :(得分:2)

  

如何从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

答案 1 :(得分:1)

您可以从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>