Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / June 2007



Tip: Looking for answers? Try searching our database.

Removing the XML declaration

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fx910 - 29 Jun 2007 19:16 GMT
[i](I accidentally posted this to the Getting Started forum - apologies for the
duplication.)[/i]

I am setting up a CMS that will take articles stored in XML and insert them
into the middle of a predefined HTML page. The concept is essentially as per
the appended code.

However, this causes two problems: first, the XML declaration is outputted.
Second, it outputs the <content></content> tags.

It is possible to overcome this by converting the XML to text with the
toString() function then doing a find and replace, but that seems so messy.

Does anyone have any ideas of how to use native CF functions to return only
the contents - tags and all - of an XML structure, but without the XML
declaration or parent tag?
[hr]

<!--- Get the XML --->
<cfsavecontent variable="originalFile">
<?xml version="1.0" encoding="utf-8"?>
<article>
    <display>
        <title>The article title</title>
        <otherElements />
    </display>
    <content>
        <h1>Major heading</h1>
        <p>First paragraph</p>
        <p>Second paragraph</p>
    </content>
</article>
</cfsavecontent>
<!--- Now we output the page --->
<html>
    <head>
    </head>
    <body>
        <cfset xmlObject    = xmlParse(trim(originalFile)) />
        <cfset articleData  = xmlSearch(xmlObject, '/article/content') />
        <!--- Now display the article contents --->
        <cfoutput>#articleData[1].XmlChildren[1]#</cfoutput>
    </body>
</html>
Ian Skinner - 29 Jun 2007 19:41 GMT
Does anyone have any ideas of how to use native CF functions to return
only the contents - tags and all - of an XML structure, but without the
XML declaration or parent tag?

If I understand your requirement correctly, I would probably just use
the CFML xmlTransform() function to process the XML data with an XSLT
string to return the formated data that I wanted to output.

This should give you an idea of what I'm talking about.

<cfsavecontent variable="someXSLT">
<?xml version="1.0" encoding="iso-8859-1"?><!--
DWXMLSource="../../../ColdFusion/playground/test.xml" --><!DOCTYPE
xsl:stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;">
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="/article/content">
    <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet></cfsavecontent>

<cfset outputString = xmlTransform(xmlDate,someXSLT)>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.