[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Nesting a XML tag in cfxml

Posted on 2009-12-16
5
Medium Priority
?
273 Views
Last Modified: 2013-12-24
I'm creating an xml document from a query called qRead:
<cfxml variable="myDoc">
     <Root>
          <cfoutput query="qRead">
               <#qRead.elementName#>#qRead.elementDescription#</#qRead.elementName#>
          </cfoutput>
     </Root>
 </cfxml>

I have a requirement to nest certain elements depending on the circumstance. For example,  if <Text> This is the text I am writing </Text> it would remain as shown. However, if the <Display> element is before a <Text> element I will need to nest as follows:
<Display>
     <Text> This is the text I am writing </Text>
<Display>

This is important because it prepares the system to display the text. Any ideas on how I can accomplish this? Can I put <cfif> logic within the <cfoutput> tag to catch the <Display> tag? Once I have it, how could I ensure that the next <Text> tag is nested within the <Display> tag?

Thanks for your help.

Let me know if I need to clarify.
0
Comment
Question by:coconutelegraph
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:azadisaryev
ID: 26069036
try the attached code.
it has not been tested, though, so may throw errors...

Azadi
<cfxml variable="myDoc">
 <Root>
 <cfset skiprownum = 0>
 <cfloop from="1" to="#qRead.recordcount#" index="row">
   <cfif row neq skiprownum>
     <#qRead['elementName'][row]#>
     <cfif rwo LT qRead.recordcount AND qRead['elementName'][row] is 'Display' AND qRead['elementName'][row+1] is 'Text'>
       <cfset skiprownum = row+1>
       <#qRead['elementName'][row+1]#>#qRead['elementDescription'][row+1]#</#qRead['elementName'][row+1]#>
     <cfelse>
       #qRead['elementDescription'][row]#
     </cfif>
     </#qRead['elementName'][row]#>
   </cfif>
 </cfloop>
 </Root>
</cfxml>

Open in new window

0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 2000 total points
ID: 26069057
sorry, had a typo and missing <cfoutput> tags in the code.
attached is corrected code, which seems to do what you need.

Azadi
<cfxml variable="myDoc">
 <Root>
 <cfset skiprownum = 0>
 <cfoutput>
 <cfloop from="1" to="#qRead.recordcount#" index="row">
   <cfif row neq skiprownum>
     <#qRead['elementName'][row]#>
     <cfif row LT qRead.recordcount AND qRead['elementName'][row] is 'Display' AND qRead['elementName'][row+1] is 'Text'>
       <cfset skiprownum = row+1>
       <#qRead['elementName'][row+1]#>#qRead['elementDescription'][row+1]#</#qRead['elementName'][row+1]#>
     <cfelse>
       #qRead['elementDescription'][row]#
     </cfif>
     </#qRead['elementName'][row]#>
   </cfif>
 </cfloop>
 </cfoutput>
 </Root>
</cfxml>
<cfdump var="#myDoc#">

Open in new window

0
 

Author Comment

by:coconutelegraph
ID: 26072463
This looks like it may work. Let me give it  go. Thanks for your help.
0
 

Author Comment

by:coconutelegraph
ID: 26072869
OK. The nesting is working great. Thanks. I have one last thing to do that is giving me fits. I need to add some cftags to the page so they execute when a form variable is present. Specifically, I need to add the follwing to the #myDoc#  xml created above:

<cfif IsDefined("form.variable")
   <cfif #form.variable# eq 1>
         execute code...
   </cfif>
</cfif>

When I insert this code  in the <cfxml variable="myDoc"> as:
<cfxml variable="myDoc">
     <cfif IsDefined("form.variable")
        <cfif #form.variable# eq 1>
           execute code...
       </cfif>
   </cfif>
 <Root>
    <cfset skipronum = 0>...

The newly added cftags do not appear in the output. Thanks and let me know if I need to create another question.
0
 

Author Comment

by:coconutelegraph
ID: 26074634
Separate question created for final post.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
What You Need to Know when Searching for a Webhost Provider
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question