473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Let user omit elements with checkboxes?

7
Thank you for even reading this...

I have an xml doc that feeds into html. I am being passed the xml via asp.net. I have used xsl to create an html. Now I'm being asked to let the user omit which elements get copied to new xml (via another xsl?). I would like to present the xml elements to my user and let them select which elements to copy onto another xml doc by clicking a checkbox. Is this possible and how?

So far I am thiking maybe I should use xsl to create a form then use another xsl somehow copies the selected elements.

Or perhaps I should use ASP.NET to select which elements to write to xml in the first place. (Well IT would have to do that as I am not allowed to touch the ASP.NET).

Please provide sample code for using a checked checkbox to copy elements.

Original XML:
Expand|Select|Wrap|Line Numbers
  1. <peoples>
  2.    <person>
  3.      <name>Stan</name>
  4.      <address>11 View St.</address>
  5.      <city>Sometown</city>
  6.      <state>CA</state>
  7.      <zip>99999</zip>
  8.      <phone>111-2222</phone>
  9.      <email>stan@domain.com</email>
  10.    </person>
  11. </peoples>
Possible Outcome:
Expand|Select|Wrap|Line Numbers
  1. <peoples>
  2.    <person>
  3.      <name>Stan</name>
  4.      <phone>111-2222</phone>
  5.    </person>
  6. </peoples>
Oct 9 '07 #1
8 1674
jkmyoung
2,057 Expert 2GB
For creating the form, you could something like:
Expand|Select|Wrap|Line Numbers
  1. <form name="elementFilter" action="html_form_action.asp" method="get">
  2.   <xsl:for-each select="/peoples/person[1]/*">
  3.     <input type="checkbox" name="{local-name()}" value="{local-name()}"/>
  4.   </xsl:for-each>
  5. </form>
Then when you receive the data from the submission, pass it to the xml accordingly.
Pseudocode: (since I don't know what processor you're using.)
foreach value sent in:
set xsl parameter with given name to true
end foreach

In your result xslt: (this could be dynamically generated too, if you don't know the names of the elements beforehand.)
Expand|Select|Wrap|Line Numbers
  1. <xsl:param name="name"/>
  2. <xsl:param name="address"/>
  3. <xsl:param name="city"/>
  4. <xsl:param name="state"/>
  5. <xsl:param name="zip"/>
  6. <xsl:param name="phone"/>
  7. <xsl:param name="email"/>
Use templates to make copying easier:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="person">
  2.   <xsl:copy>
  3.     <xsl:apply-templates/>
  4.   </xsl:copy>
  5. </xsl:template>
Then in each of the corresponding templates:

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="name">
  2.   <xsl:if test="$name != ''">
  3.     <xsl:copy-of select="."/>
  4.   </xsl:if>
  5. </xsl:template>
Note, the code written assumes a flat, table-like structure to your data. It can be modified if your data is imbricated.
Oct 10 '07 #2
swsdam
7
Thanks you so much for your help jkmyoung!

I have only gotten as far as passing the values to the form. We are using the .NET Framework XML core classes (MSXML?) to process the xsl.

But I have a question about the value in the initial form. So far I've modified the form so it gives the user a preview of the values in the form.
Expand|Select|Wrap|Line Numbers
  1.     <xsl:for-each select="/peoples/person">
  2.         <p>
  3.         <xsl:for-each select="*">
  4.             <input type="checkbox" name="{local-name()}" value="{local-name()}" checked="checked" />
  5.             <label for="{local-name()}">
  6.             Print <xsl:value-of select="local-name()" />: 
  7.                        <xsl:value-of select="." /> </label><br />
  8.         </xsl:for-each>
  9.         </p>
  10.     </xsl:for-each>
  11.  
But I notice the the value is not the value in the original xml. Where Does the value for the parameter pull the value from the original xml? Is this happening when I set the xsl parameters in the processor? Is there something I'm not understanding?

Also, would approach still work with elements and attributes?

For creating the form, you could something like:
Expand|Select|Wrap|Line Numbers
  1. <form name="elementFilter" action="html_form_action.asp" method="get">
  2.   <xsl:for-each select="/peoples/person[1]/*">
  3.     <input type="checkbox" name="{local-name()}" value="{local-name()}"/>
  4.   </xsl:for-each>
  5. </form>
Then when you receive the data from the submission, pass it to the xml accordingly.
Pseudocode: (since I don't know what processor you're using.)
foreach value sent in:
set xsl parameter with given name to true
end foreach

In your result xslt: (this could be dynamically generated too, if you don't know the names of the elements beforehand.)
Expand|Select|Wrap|Line Numbers
  1. <xsl:param name="name"/>
  2. <xsl:param name="address"/>
  3. <xsl:param name="city"/>
  4. <xsl:param name="state"/>
  5. <xsl:param name="zip"/>
  6. <xsl:param name="phone"/>
  7. <xsl:param name="email"/>
Oct 11 '07 #3
jkmyoung
2,057 Expert 2GB
What you're doing looks like it should retrieve the original values. I'd check the filepaths, eg the place where you are loading the original xml to be processed. Parameters shouldn't affect this at all.

On a side note, do you only ever have one person node?
Oct 12 '07 #4
swsdam
7
What you're doing looks like it should retrieve the original values. I'd check the filepaths, eg the place where you are loading the original xml to be processed. Parameters shouldn't affect this at all.

On a side note, do you only ever have one person node?
Let me ask another way. Am I passing the values to the parameters when I post the form (and the check boxes) to the processing asp page? Otherwise, I'm not sure how the value gets assigned to the parameter. I looked up everything I could about parameters and I did not find a way to set a parameter to true. I assume you mean for me to define it in the XSL.

Can you tell me more about is happening when the form data gets passed to the processor? Or maybe just why I'm setting the parameter to tru?


Pseudocode: (since I don't know what processor you're using.)
Expand|Select|Wrap|Line Numbers
  1. foreach value sent in:
  2. set xsl parameter with given name to true
  3. end foreach
And no there are multiple people nodes. Which is why I'm adding a <p> in between the for statement.
Oct 12 '07 #5
jkmyoung
2,057 Expert 2GB
The 'true' could really be any value.
Just set it to anything which isn't the empty string, so that your xslt recognizes that the parameter has been set.
Oct 17 '07 #6
swsdam
7
What do you mean pass the results back to the xml? And can the pseudo code be accomplished with MSXML and/or ASP.NET?

Originally Posted by jkmyoung
Pseudocode: (since I don't know what processor you're using.)
Code: ( text )
1. foreach value sent in:
2. set xsl parameter with given name to true
3. end foreach
Nov 10 '07 #7
jkmyoung
2,057 Expert 2GB
What do you mean pass the results back to the xml? And can the pseudo code be accomplished with MSXML and/or ASP.NET?
I realized over the weekend a much simpler method.
Instead of creating a complicated parameter passing scheme, you could instead create a secondary xml.

Based on the checkboxes, create a secondary xml like so:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2.     <element>peoples</element>
  3.     <element>person</element>
  4.     <element>name</element>
  5.     <element>phone</element>
  6. </root>
(copy.xml)

Then, apply this xsl to your original xml, referencing copy.xml so that you know which nodes to copy over.
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="copy" select="document('copy.xml')/root/element"/>
  2. <xsl:template match="*">
  3.     <xsl:if test="$copy[. = local-name(current())]">
  4.     <xsl:copy>
  5.         <xsl:apply-templates/>
  6.     </xsl:copy>
  7.     </xsl:if>
  8. </xsl:template>
This should give you this as output:
Expand|Select|Wrap|Line Numbers
  1. <peoples>
  2.     <person>
  3.         <name>Stan</name>
  4.         <phone>111-2222</phone>
  5.     </person>
  6. </peoples>
I hope this solution is easier to implement and get your head around.
Nov 13 '07 #8
swsdam
7
I realized over the weekend a much simpler method.
Instead of creating a complicated parameter passing scheme, you could instead create a secondary xml.

...

I hope this solution is easier to implement and get your head around.
Yes. This is much easier for me to understand and implement. Thanks so much! You Rock!
Nov 13 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Don W. | last post by:
Is it possible to open a browser window from a javascript for the purpose of gathering user input on a form and then use data from the form in the javascript? How do you get the data from the form...
4
by: Logico | last post by:
Hi everybody, I need to do a function in javascript to check or uncheck all checkboxes with the same id. I want this function to work in every form and every page of my site, as I will use the same...
22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
4
by: omidmottaghi | last post by:
I need to disable/enable form elements in my form. the code i was developed works fine in FF, but in IE, its behaviour is very strange!! in the form, we have a lot of checkboxes, all of them...
12
by: Tobias Froehlich | last post by:
Hi, I have a form called form1 on which there are a lot of checkBoxes (they form a 8x6 field). I'd like to create a class in a seperate .cs file which gives some control over these checkBoxes...
5
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
2
by: Charles Law | last post by:
I have a complex object that I am serializing, and I would like to omit elements that have a default value. For example, if I have a class as follows Public Class Test Private m_Name As...
10
by: tadisaus2 | last post by:
Hello, I want to have a user to check at least 2 check boxes and NO more than 2 boxes. I have different checkbox names because I stored each nam on different field. I tried this code but nothing...
0
by: DIOS | last post by:
I am using the My.Settings class to save the state of user options in a tab control on the main form. One tab is for the main data elements and the other tab holds the user options which consist of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.