#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    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> &lt;leaf3&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf3&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf5">
    		<xsl:text> &lt;leaf5&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf5&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf">
    		<xsl:text> &lt;leaf&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf1">
    		<xsl:text> &lt;leaf1&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf1&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf2">
    		<xsl:text> &lt;leaf2&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf2&gt; </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 &lt;leaf&gt;33333&lt;/leaf&gt; datan datan datan datan datan datan &lt;leaf&gt;55555&lt;/leaf&gt; aa ss aa &lt;leaf5&gt;55555&lt;/leaf5&gt; wert wert"/>
    </root>

    Comments on this post

    • fardadja agrees : Worked like a charm. Very fast and accurate response. Thanks!
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep 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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    1

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

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


    2

    make more template and change the leaf template

    Code:
    <xsl:template match="leaf">
    	<xsl:text> &lt;leaf</xsl:text>
    	<xsl:apply-templates select="@*"/>
    	<xsl:text>&gt;</xsl:text>
    	<xsl:value-of select="."/>
    	<xsl:text>&lt;/leaf&gt; </xsl:text>		
    </xsl:template>
    
    
    <xsl:template match="@*">
    	<xsl:text> </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>=&amp;quot;</xsl:text><xsl:value-of select="."/>
    		<xsl:text>&amp;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> &lt;leaf3&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf3&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf5">
    		<xsl:text> &lt;leaf5&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf5&gt; </xsl:text>
    	</xsl:template>	
    	<xsl:template match="leaf">
    		<xsl:text> &lt;leaf</xsl:text>
    		<xsl:apply-templates select="@*"/>
    		<xsl:text>&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf&gt; </xsl:text>		
    	</xsl:template>	
    	<xsl:template match="leaf1">
    		<xsl:text> &lt;leaf1&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf1&gt; </xsl:text>
    	</xsl:template>
    	<xsl:template match="leaf2">
    		<xsl:text> &lt;leaf2&gt;</xsl:text>
    		<xsl:value-of select="."/>
    		<xsl:text>&lt;/leaf2&gt; </xsl:text>
    	</xsl:template>	
    	<xsl:template match="@*">
    	<xsl:text> </xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>=&amp;quot;</xsl:text><xsl:value-of select="."/>
    		<xsl:text>&amp;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 &lt;leaf id=&quot;5&quot; data=&quot;data&quot;&gt;33333&lt;/leaf&gt; datan datan datan datan datan datan &lt;leaf&gt;55555&lt;/leaf&gt; aa ss aa &lt;leaf5&gt;55555&lt;/leaf5&gt; wert wert"/>
    </root>
    sorry for my bad english
    Last edited by xml-profi; March 7th, 2011 at 04:45 AM.
    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

IMN logo majestic logo threadwatch logo seochat tools logo