Remove xml declarations when using xslt
Copy link to clipboard
Copied
Hi, i have created an xslt which transforms an xml file and then imports the information into a mysql database. It works and imports the information into the database but it also brings across the xml delcarations and the node names.
Is there a coldfusion way to remove these declarations, all i want is the information itself brought into the database.
Thanks
Chris
Copy link to clipboard
Copied
G'day
One doesn't do it via CF, one does it via the XSLT itself.
Have a read of this: http://www.w3schools.com/xsl/el_output.asp
--
Adam
Copy link to clipboard
Copied
Hi Adam,
thanks for the reply. I did go to the site and tried using that option but its still including the declarations, could it be a configuration problem?
Thanks, Chris.
Copy link to clipboard
Copied
That's weird, cos it works for me.
Can you post a brief code block (with sample XML and XSL) that demonstrates it not working? I'll have a look (although perhaps not until tomorrow now, as I've about done my dash for these forums for the day).
--
Adam
Copy link to clipboard
Copied
Hi Adam,
here is the code from the xsl file:
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="data/itemfeed.xml" --><!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<xsl:for-each select="Items/Item[ItemType='Standard']">
<xsl:value-of select="EANNumber"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
And here is some sample data from the XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<Items>
<Item>
<ItemType>Standard</ItemType>
<EANNumber>111222333444</EANNumber>
</Item>
</Items>
Thanks alot, i appreciate the help.
Chris
Copy link to clipboard
Copied
Ah, OK I see what you mean. I think.
Your problem is that you're telling it to put all that extraneous stuff in there.
When I run your code, the result is this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>111222333444</body></html>
I presume you are only wanting this:
111222333444
If that's the case, get rid of all the doctype/html/head/body crap form your XSL. If you don't want it, don't put it in there!
Or are we talking @ cross-purposes here?
--
Adam
Copy link to clipboard
Copied
Hi Adam,
the HTML, Body tags etc are fine, they can stay, its the actual tags that surround the results that i want to get rid of. When i look at the source code, the results show up like this: <?xml version="1.0" encoding="UTF-8"?><EANNumber>111222333444</EANNumber>. I only want the 111222333444, i dont want the surrounding tags.
Thanks
Chris
Copy link to clipboard
Copied
Right.
Well I think you better post your actual code in its entirety then. Because when I slap <cfxml> tags around each of the XSL and XML you provide, and do an xmlTransForm() on them, I just get the numbers (and all the other HTML crap), but no <EANNumber> tags. I get *exactly* what I posted before.
My code is thus:
<cfxml variable="xXsl">
[YOUR XSL HERE]
</cfxml>
<cfxml variable="xXml">
[YOUR XML HERE]
</cfxml>
<cfoutput>#xmlTransform(xXml, xXsl)#</cfoutput>
That outputs what I cited above.
--
Adam

