XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 5th, 2011, 10:30 PM
fardadja fardadja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 fardadja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 29 sec
Reputation Power: 0
XSL nesting in ASP.NET

Hi everyone,

I'm fairly new to XSL. This is the situation
I have an XML file with data in it laid out as:

<root>

<element>

<leaf1>
data
</leaf1>

<leaf2>
data
</leaf2

<element2>
data data data
data data data

<leaf3>
</leaf3>

data data data
data data data
</element2>

</element>

</root>


This is what I'm trying to achieve:
I'm using an XSL file to 'parse' this XML in ASP.NET. The XSL transforms the document as:

<root>

<element leaf1="" leaf2="" element3="">
</element>

</root>

so I can use it in XMLDataSource.

I've used this before with no problem. Even now I can show all the data with no problem. The dilemma is I need leaf3 to be imported into ASP.NET as it is. I mean element2 should show as:

data
<leaf3>
info
</leaf3>
data

NOT as:

data
info
data

Unfortunately, I am getting the latter. So as you've probably guessed it by now, the data in leaf3 (element2 contains more of this type of node) is a kind of mark up language which I want to process in ASP.NET. So I need that bit of the document imported as it is.

I'd very much like this imported into XMLDataSource control specifically for various reasons.

I've tried playing around with various XSL commands but have failed so far. I guess there should be a simple solution to this (possibly to do with copy-of or copy) but I don't know how to do it. There is no point in me posting any code I guess. But please let me know if you want me to.

I would very much appreciate any help on this.

Thank you very much!
F

Reply With Quote
  #2  
Old March 6th, 2011, 01:14 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 190 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 7 h 8 m 25 sec
Reputation Power: 48
Send a message via ICQ to xml-profi
which xml do you mean

Code:
<root>

	<element>

		<leaf1>data</leaf1>

		<leaf2>data</leaf2>

		<element2>data data data
                                  data data data
			<leaf3>
			</leaf3>
                                  data data data
                                  data data data
                </element2>
	</element>
</root>



where is element3

Code:
<element leaf1="" leaf2="" element3="">
</element>

i can not see it
__________________
Helmut Hagemann Germany

fallen to the bottom of the facts?
I reach my hand and we go together


wer lesen und google kann ist klar im Vorteil
who read and google is able is clear in the advantage

Reply With Quote
  #3  
Old March 6th, 2011, 01:25 AM
fardadja fardadja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 fardadja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by xml-profi
which xml do you mean

Code:
<root>

	<element>

		<leaf1>data</leaf1>

		<leaf2>data</leaf2>

		<element2>data data data
                                  data data data
			<leaf3>
			</leaf3>
                                  data data data
                                  data data data
                </element2>
	</element>
</root>



where is element3

Code:
<element leaf1="" leaf2="" element3="">
</element>

i can not see it


oops, that was a typo. It should have been element2.

So I want this as output:

<element leaf1="whatever" leaf2="whatever" element2="data<leaf3>data</leaf3>data">
</element>

So you see, I want leaf3 to preserve its annotations even tho it appears as an attribute value of element.

By the way, leaf3 won't have any attributes of its own but may have sub-elements. But I guess if we can manage the problem with leaf3 itself, it should be trivial (or maybe no action required at all?) to keep its children in the attribute value. Am I correct in assuming this? So for instance:

<element leaf1="whatever" leaf2="whatever" element2="data<leaf3>data<leaf4>data<leaf5>data</leaf5>data</leaf4>data</leaf3>data">
</element>


Thank you for your reply by the way

Reply With Quote
  #4  
Old March 6th, 2011, 03:57 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 190 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 7 h 8 m 25 sec
Reputation Power: 48
Send a message via ICQ to xml-profi
xml to test
Code:

<root>

	<element>

		<leaf1>datal1</leaf1>

		<leaf2>datal2</leaf2>

		<element2>datav datav datav
datav datav datav

			<leaf>33333</leaf>datan datan datan
datan datan datan <leaf>55555</leaf>aa ss aa<leaf5>55555</leaf5>wert wert</element2>
	</element>
</root>

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="/">
		<root>
			<xsl:apply-templates select="root"/>
		</root>
	</xsl:template>
	<xsl:template match="root">
		<xsl:apply-templates select="element"/>
	</xsl:template>
	<xsl:template match="element">
		<xsl:variable name="e2">
			<xsl:apply-templates select="child::element2"/>
		</xsl:variable>
		<xsl:element name="element">
			<xsl:attribute name="leaf1">
				<xsl:value-of select="leaf1"/>
			</xsl:attribute>
			<xsl:attribute name="leaf2">
				<xsl:value-of select="leaf2"/>
			</xsl:attribute>
			<xsl:attribute name="element2">
				<xsl:value-of select="normalize-space($e2)"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>
        <!-- discripted the node which found in element2 -->
	<xsl:template match="leaf3">
		<xsl:text> <leaf3></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf3> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf5">
		<xsl:text> <leaf5></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf5> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf">
		<xsl:text> <leaf></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf1">
		<xsl:text> <leaf1></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf1> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf2">
		<xsl:text> <leaf2></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf2> </xsl:text>
	</xsl:template>
</xsl:stylesheet>



result

Code:
<?xml version='1.0' encoding='UTF-8' ?>
<root>
  <element leaf1="datal1" leaf2="datal2" element2="datav datav datav datav datav datav <leaf>33333</leaf> datan datan datan datan datan datan <leaf>55555</leaf> aa ss aa <leaf5>55555</leaf5> wert wert"/>
</root>
Comments on this post
fardadja agrees: Worked like a charm. Very fast and accurate response. Thanks!

Reply With Quote
  #5  
Old March 6th, 2011, 12:32 PM
fardadja fardadja is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 fardadja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 29 sec
Reputation Power: 0
Thank you so much!!! I had been wrestling with the problem for a while.

However I'm not sure why I couldn't use the last part of your code as you wrote it:

Code:
<!-- discripted the node which found in element2 -->
	<xsl:template match="leaf3">
		<xsl:text> <leaf3></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text><leaf3> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf5">
		<xsl:text> <leaf5></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text><leaf5> </xsl:text>
	</xsl:template>
	...




I was getting parsing errors that for instance </text> is expected before <leaf3> (in the third line). I tried using disable-output-escaping as well but all the same.

So I finally ended up using <![CDATA...]]> which works fine.

The rest of the code worls like a charm! Now I have to google-read your code to understand some parts (as I said, I'm new to XSLT).

Thanks to you, my problem is solved now however I'm trying to understand the logic here so I would appreciate any clarification on the following three points:

First, any idea why I couldn't use <xsl:text>?

Second, let's say for instance leaft3 has some attributes. Is it possible to include its attributes as well so we get this:


<element leaf1="whatever" leaf2="whatever" element2="data<leaf3 id="ID">data</leaf3>data">
</element>

I guess we probably have to use some sort of espace characters here to avoid the conflict between " characters, right?

Third, is there any way not to include the description found in e2? So to omit the data in
<!-- discripted the node which found in element2 -->
in the code? So I guess I'm asking if there is a way to use a variable instead of "leaf3" in:

<!-- discripted the node which found in element2 -->
<xsl:template match="leaf3">
<xsl:text> <leaf3></xsl:text>
<xsl:value-of select="."/>
<xsl:text><leaf3> </xsl:text>
</xsl:template>
...
...
We possibly can (I guess) but I'm not very familiar with the syntax of XSLT.

As I said, these three points are extra. You solved my problem however I would like to have some more insight.

Thanks again!
F

Reply With Quote
  #6  
Old March 7th, 2011, 03:33 AM
xml-profi xml-profi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Posts: 190 xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level)xml-profi User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 7 h 8 m 25 sec
Reputation Power: 48
Send a message via ICQ to xml-profi
1

in xml ist not allowed to use in text " ' < > & http://en.wikipedia.org/wiki/Charac...codings_in_HTML
so xml authors make the &quote;&apos; &lt; &gt; &amp;

when you use <[CDATA[ ]]> then the Parser saw not the
&quote;&apos; &lt; &gt; &amp;


2

make more template and change the leaf template

Code:
<xsl:template match="leaf">
	<xsl:text> <leaf</xsl:text>
	<xsl:apply-templates select="@*"/>
	<xsl:text>></xsl:text>
	<xsl:value-of select="."/>
	<xsl:text></leaf> </xsl:text>		
</xsl:template>


<xsl:template match="@*">
	<xsl:text> </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>=&quot;</xsl:text><xsl:value-of select="."/>
		<xsl:text>&quot;</xsl:text>
	</xsl:template>


xml to test
Code:
<root>
	<element>
		<leaf1>datal1</leaf1>
		<leaf2>datal2</leaf2>
		<element2>datav datav datav
datav datav datav
			<leaf id="5" data="data">33333</leaf>datan datan datan
datan datan datan <leaf>55555</leaf>aa ss aa<leaf5>55555</leaf5>wert wert</element2>
	</element>
</root>


Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
	<xsl:template match="/">
		<root>
			<xsl:apply-templates select="root"/>
		</root>
	</xsl:template>
	<xsl:template match="root">
		<xsl:apply-templates select="element"/>
	</xsl:template>
	<xsl:template match="element">
		<xsl:variable name="e2">
			<xsl:apply-templates select="child::element2"/>
		</xsl:variable>
		<xsl:element name="element">
			<xsl:attribute name="leaf1">
				<xsl:value-of select="leaf1"/>
			</xsl:attribute>
			<xsl:attribute name="leaf2">
				<xsl:value-of select="leaf2"/>
			</xsl:attribute>
			<xsl:attribute name="element2">
				<xsl:value-of select="normalize-space($e2)"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>	
	<xsl:template match="leaf3">
		<xsl:text> <leaf3></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf3> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf5">
		<xsl:text> <leaf5></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf5> </xsl:text>
	</xsl:template>	
	<xsl:template match="leaf">
		<xsl:text> <leaf</xsl:text>
		<xsl:apply-templates select="@*"/>
		<xsl:text>></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf> </xsl:text>		
	</xsl:template>	
	<xsl:template match="leaf1">
		<xsl:text> <leaf1></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf1> </xsl:text>
	</xsl:template>
	<xsl:template match="leaf2">
		<xsl:text> <leaf2></xsl:text>
		<xsl:value-of select="."/>
		<xsl:text></leaf2> </xsl:text>
	</xsl:template>	
	<xsl:template match="@*">
	<xsl:text> </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>=&quot;</xsl:text><xsl:value-of select="."/>
		<xsl:text>&quot;</xsl:text>
	</xsl:template>
</xsl:stylesheet>


result
Code:
<?xml version='1.0' encoding='UTF-8' ?>
<root>
  <element leaf1="datal1" leaf2="datal2" element2="datav datav datav datav datav datav <leaf id="5" data="data">33333</leaf> datan datan datan datan datan datan <leaf>55555</leaf> aa ss aa <leaf5>55555</leaf5> wert wert"/>
</root>

sorry for my bad english

Last edited by xml-profi : March 7th, 2011 at 03:45 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSL nesting in ASP.NET

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap