Reputation: 699
I am using a following PHP routine to transform XML using XSL into HTML. The problem is that during the transformation xmlns namespaces are getting inserted as attributes into the h2 element.
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 (from link above):
<?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>
Resulting HTML code:
<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>
My question is: how do I get rid of xmlns attributes from the h2 element? Why is it even there?
Upvotes: 0
Views: 140
Reputation: 116993
how do I get rid of xmlns attributes from the h2 element?
Try changing this:
<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">
to:
<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">
Untested, because no input example was provided. BTW, a namespace declaration is not an attribute.
Why is it even there?
Because apparently your input nodes are in a namespace and you need those prefixes to address them - see: XSLT Transform doesn't work until I remove root node
Upvotes: 3
Reputation: 1202
You could remove the namespaces from your XSL stylesheet and ignore namespaces in your for-each select to avoid namespaces in your output, e.g.:
<?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>
Output then looks like:
<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>
Upvotes: 1