XSL Transform

Discussion in 'XML' started by Ravi, Nov 3, 2003.

  1. Ravi

    Ravi Guest

    I am new to XSL. I have a XML document something like

    <abc>
    xyz
    <def> 1 </def>
    <def> 2 </def>
    <def> 3 </def>
    <def> 4 </def>
    <def> 5 </def>
    </abc>

    I am trying to get the value of the abc node which in this case is xyz.
    However if I try to get the value using <xsl:value-of select="."/> in a
    template matching abc I get

    xyz 1 2 3 4 5

    which is not what I want. Is there someway to select only the value of
    the parent node and then display each child node's value on say a
    separate line? I tried using concat and substring-before and though it
    works its not a good solution as I do not know how many children abc has
    in advance (besides being very laborious). Any suggestions will be
    highly appreciated.

    TIA.
     
    Ravi, Nov 3, 2003
    #1
    1. Advertising

  2. Ravi

    Dino Morelli Guest

    On Mon, 3 Nov 2003, Ravi wrote:

    >I am new to XSL. I have a XML document something like
    >
    ><abc>
    >xyz
    ><def> 1 </def>
    ><def> 2 </def>
    ><def> 3 </def>
    ><def> 4 </def>
    ><def> 5 </def>
    ></abc>
    >
    >I am trying to get the value of the abc node which in this case is xyz.
    >However if I try to get the value using <xsl:value-of select="."/> in a
    >template matching abc I get
    >
    >xyz 1 2 3 4 5
    >
    >which is not what I want. Is there someway to select only the value of
    >the parent node and then display each child node's value on say a
    >separate line? I tried using concat and substring-before and though it
    >works its not a good solution as I do not know how many children abc has
    >in advance (besides being very laborious). Any suggestions will be
    >highly appreciated.
    >


    To get all of the text values of all of the children of <abc> on
    separate lines you can loop through all children:

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="text" />

    <xsl:template match="/">
    <xsl:for-each select="*">
    <xsl:value-of select="." />
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>


    To get just the single text value of <abc> without the <def> children at
    all you can use the text() function:

    <?xml version="1.0" encoding="utf-8"?>

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="text" />

    <xsl:template match="/">
    <xsl:value-of select="/abc/text()" />
    </xsl:template>

    </xsl:stylesheet>


    --
    Dino Morelli .~.
    http://www.debian.org Debian GNU/Linux /V\
    /( )\
    ^^-^^
     
    Dino Morelli, Nov 3, 2003
    #2
    1. Advertising

  3. "Ravi" <> wrote in message
    news:bo6btd$74d$...
    > I am new to XSL. I have a XML document something like
    >
    > <abc>
    > xyz
    > <def> 1 </def>
    > <def> 2 </def>
    > <def> 3 </def>
    > <def> 4 </def>
    > <def> 5 </def>
    > </abc>
    >
    > I am trying to get the value of the abc node which in this case is xyz.


    No, the string value of an element is the concatenation of all of its
    descendent text nodes:

    From the XPath spec:

    "The string-value of an element node is the concatenation of the
    string-values of all text node descendants of the element node in document
    order."

    http://www.w3.org/TR/xpath#element-nodes




    > However if I try to get the value using <xsl:value-of select="."/> in a
    > template matching abc I get
    >
    > xyz 1 2 3 4 5


    This is the correct string value of the element (or root node) according to
    the XPath spec.

    >
    > which is not what I want. Is there someway to select only the value of
    > the parent node and then display each child node's value on say a
    > separate line?


    By "value of the parent node" I guess you mean the value of its first text
    node child?

    Yes, this transformation produces the results you want:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >

    <xsl:eek:utput method="text"/>
    <xsl:strip-space elements="*" />

    <xsl:template match="text()">
    <xsl:value-of select="."/>
    <xsl:text>
    </xsl:text>
    </xsl:template>
    </xsl:stylesheet>



    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
     
    Dimitre Novatchev, Nov 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    0
    Views:
    1,054
    KathyB
    Jun 25, 2003
  2. Cowboy \(Gregory A. Beamer\)

    Re: How do i transform an xml with 2 xsl files?

    Cowboy \(Gregory A. Beamer\), Jul 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    308
    Showjumper
    Jul 3, 2003
  3. Showjumper
    Replies:
    1
    Views:
    316
    Showjumper
    Jul 3, 2003
  4. suzy
    Replies:
    0
    Views:
    569
  5. Replies:
    1
    Views:
    3,686
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page