Here’s my problem. I am building a web app that submits xml data to a govt application that returns xml data back that I then parse via xslt into html for viewing. The problem is that the returned xml has 2 (two) blank lines before the declaration
<?xml version='1.0' standalone='yes'?>
Well xslt doesn’t like that and won’t parse the xml into html. I know this is the problem because if I manually remove the 2 (two) blank lines via an editing program like notepad, the transformation works perfectly. I have no control over how the returned xml data is formatted so I need to find a way to first remove those 2 (two) blank lines before continuing the parsing.
So here’s my xml (imagine 2 blank lines at the beginning):
<?xml version='1.0' standalone='yes'?>
<FHAENTITYDATA FHAVersionID = '1.0'>
<PROCESSSTATUS>
<ProcessStatusCode>Success</ProcessStatusCode>
<ProcessStatusMessage sCode = '1'>Returned 1 zip code record(s)</ProcessStatusMessage>
</PROCESSSTATUS>
</FHAENTITYDATA>
And here’s my xsl:
<?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>
<head>
<link rel="stylesheet" href="../css/fhac.css" type="text/css"/>
</head>
<body style="font-family:verdana;">
<div id="content">
<div id="fieldset">
<xsl:for-each select="//ProcessStatusCode">
<tr><td><b><xsl:value-of select="."/></b></td></tr>
</xsl:for-each>
<xsl:for-each select="//ProcessStatusMessage">
<tr><td><b><xsl:value-of select="."/></b></td></tr>
</xsl:for-each>
</table>
</div></div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
My question is, is there any way to remove those leading lines before or during the xslt?
The answer to your question is “no”.
the XML declaration must be the first thing the XML parser reads, it’s less upsetting for the parser to have no XML declaration than to have it elsewhere but in the first line of a file (/string/buffer/ whatever).
The answer to your problem is indeed a trim. The exact implementation depending on what else you can work with (php in this case). I’d suggest you only remove blank lines before the XML declaration, rather than removing all blank lines, since there might be blank lines in text elements that you may want to preserve.