XML/XSL/XPATH how to get a element value

Discussion in 'XML' started by Kniffel, Sep 7, 2007.

  1. Kniffel

    Kniffel Guest

    Hi everyone

    I do a xsl-transformation. And I try to get a Attribute, but I cannot
    get it. My XML/XSL experience is not very good at the moment.

    I have something like this:
    TEST.xml
    --------------
    <?xml version="1.0" encoding="utf-8"?>
    <Main>
    <DisplayTime Aktiviert="True" />
    <PrintTime Aktiviert="True" />
    </Main>

    TEST.xsl
    -------------
    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">
    <xsl:eek:utput method="html" version="1.0" encoding="UTF-8"
    indent="yes" />

    <xsl:template match="/Main">
    <table class="std" width="700px">
    <form name="form" action="/Address" method="get"">
    <tr><td colspan="2" class="std">
    <xsl:apply-templates select="DisplayTime"/>
    <xsl:apply-templates select="PrintTime"/>
    </td>
    </tr></form></table>
    </xsl:template>

    <xsl:template name="DisplayTime" match="DisplayTime">
    <xsl:variable name="wert" select="name()" />
    <xsl:for-each select="@*">
    <xsl:variable name="DDTattr" select="name()" />
    <xsl:variable name="DDTattrwert" select="." />
    <xsl:choose>
    <xsl:when test="$DDTattrwert='True'">
    <input type="checkbox" name="DisplayTime_{$DDTattr}"
    checked="checked">
    Display Time
    </input>
    </xsl:when>
    </xsl:choose>
    </xsl:template>

    <xsl:template name="PrintTime" match="PrintTime">
    <xsl:variable name="wert" select="name()" />
    <xsl:for-each select="@*">
    <xsl:variable name="PTattr" select="name()" />
    <xsl:variable name="PTattrwert" select="." />
    /****** Here I need a choose/when on the Element 'Aktiviert' of
    DisplayTime: ******/
    <xsl:choose>
    <xsl:when test="//DisplayTime/Aktiviert='True'">
    /****** But I don't know how I can get it! *****/
    <xsl:choose>
    <xsl:when test="$DDTattrwert='True'">
    <input type="checkbox"
    name="DisplayTime_{$DDTattr}" checked="checked">
    Display Time
    </input>
    </xsl:when>
    </xsl:choose>
    </xsl:when>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>

    How does the statement have to look like to get the Value of the
    DisplayTime element when I am in PrintTime?

    Greetings
    Kniffel
     
    Kniffel, Sep 7, 2007
    #1
    1. Advertising

  2. Kniffel

    Pavel Lepin Guest

    Kniffel <> wrote in
    <>:
    > I do a xsl-transformation. And I try to get a Attribute,
    > but I cannot get it. My XML/XSL experience is not very
    > good at the moment.
    >
    > I have something like this:
    > TEST.xml
    > --------------
    > <?xml version="1.0" encoding="utf-8"?>
    > <Main>
    > <DisplayTime Aktiviert="True" />
    > <PrintTime Aktiviert="True" />
    > </Main>
    >
    > How does the statement have to look like to get the Value
    > of the DisplayTime element when I am in PrintTime?


    Read W3C's XPath 1.0 spec. Section 2.2, Axes, is of
    particular interest in your predicament.

    --
    This chickenus crossed the roadus while yodelingus.
     
    Pavel Lepin, Sep 7, 2007
    #2
    1. Advertising

  3. Kniffel

    Kniffel Guest

    > Read W3C's XPath 1.0 spec. Section 2.2, Axes, is of
    > particular interest in your predicament.
    >


    Oh I read this some Hours ago, but I can´t get the clue!
    I tested with preceding and peceding-sibling, but I think I didn´t
    have the right syntax.
    Everytime I get a Error "The stylesheet does not contain a document
    element. ...maybe empty, or not well-formed..."

    Do you have a little example for me?
     
    Kniffel, Sep 7, 2007
    #3
  4. Kniffel wrote:

    > <Main>
    > <DisplayTime Aktiviert="True" />
    > <PrintTime Aktiviert="True" />
    > </Main>


    So the root element is the Main element with two child elements
    DisplayTime and PrintTime having an attribute Aktiviert.

    > <xsl:template name="PrintTime" match="PrintTime">
    > <xsl:variable name="wert" select="name()" />
    > <xsl:for-each select="@*">
    > <xsl:variable name="PTattr" select="name()" />
    > <xsl:variable name="PTattrwert" select="." />
    > /****** Here I need a choose/when on the Element 'Aktiviert' of
    > DisplayTime: ******/


    Aktiviert is not an element, it is an attribute of DisplayTime which is
    a sibling of PrintTime. So you can go to the common parent and then
    access the element and its attribute e.g.
    ../DisplayTime/@Aktiviert
    or you can access the sibling e.g
    preceding-sibling::DisplayTime/@Aktiviert



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 7, 2007
    #4
  5. Kniffel

    Kniffel Guest

    Things I tried:

    /preceding-sibling:Aktiviert
    preceding-sibling/Aktiviert
    /preceding-sibling::DisplayDrivingTime/@Aktiviert
    /preceding-sibling::DisplayDrivingTime/attribute::Aktiviert
    DisplayDrivingTime/@Aktiviert
    preceding-sibling::DisplayDrivingTime[attribute::Aktiviert='False']

    still have no clue about XPATH, that´s why I will test more things...
     
    Kniffel, Sep 7, 2007
    #5
  6. Kniffel

    Pavel Lepin Guest

    Kniffel <> wrote in
    <>:
    >> Read W3C's XPath 1.0 spec. Section 2.2, Axes, is of
    >> particular interest in your predicament.

    >
    > Oh I read this some Hours ago, but I can´t get the clue!
    > I tested with preceding and peceding-sibling, but I think
    > I didn´t have the right syntax.
    > Everytime I get a Error "The stylesheet does not contain a
    > document element. ...maybe empty, or not well-formed..."


    Your stylesheet is riddled with well-formedness bugs. I
    recommend you drop whatever you're doing right now and read
    a basic XML tutorial. Then an XPath tutorial. Then some
    sort of introductory XSLT reading. Then get an XML parser
    and an XSLT processor somewhere (I normally recommend
    xmllint and xsltproc--those are command-line tools that
    come with libxml2/libxslt). Armed with your newfound
    knowledge and tools try debugging your stylesheet. If
    there's still something you don't understand, come back
    with a more specific request.

    --
    This chickenus crossed the roadus while yodelingus.
     
    Pavel Lepin, Sep 7, 2007
    #6
  7. Kniffel

    Kniffel Guest

    On 7 Sep., 16:13, Kniffel <> wrote:
    > Things I tried:
    >
    > /preceding-sibling:Aktiviert
    > preceding-sibling/Aktiviert
    > /preceding-sibling::DisplayDrivingTime/@Aktiviert
    > /preceding-sibling::DisplayDrivingTime/attribute::Aktiviert
    > DisplayDrivingTime/@Aktiviert
    > preceding-sibling::DisplayDrivingTime[attribute::Aktiviert='False']
    >
    > still have no clue about XPATH, that´s why I will test more things...


    And now I tried your ways:
    preceding-sibling::DisplayTime/@Aktiviert
    .../DisplayTime/@Aktiviert=

    Still getting the same error...
     
    Kniffel, Sep 7, 2007
    #7
  8. Kniffel

    Kniffel Guest

    On 7 Sep., 16:18, Pavel Lepin <> wrote:
    > Kniffel <> wrote in
    > <>:
    >
    > >> Read W3C's XPath 1.0 spec. Section 2.2, Axes, is of
    > >> particular interest in your predicament.

    >
    > > Oh I read this some Hours ago, but I can´t get the clue!
    > > I tested with preceding and peceding-sibling, but I think
    > > I didn´t have the right syntax.
    > > Everytime I get a Error "The stylesheet does not contain a
    > > document element. ...maybe empty, or not well-formed..."

    >
    > Your stylesheet is riddled with well-formedness bugs. I
    > recommend you drop whatever you're doing right now and read
    > a basic XML tutorial. Then an XPath tutorial. Then some
    > sort of introductory XSLT reading. Then get an XML parser
    > and an XSLT processor somewhere (I normally recommend
    > xmllint and xsltproc--those are command-line tools that
    > come with libxml2/libxslt). Armed with your newfound
    > knowledge and tools try debugging your stylesheet. If
    > there's still something you don't understand, come back
    > with a more specific request.
    >
    > --
    > This chickenus crossed the roadus while yodelingus.


    My example I showed in my first post, is really minimal to the things
    I have running!
    The real XSL-file have 320 lines of code. Working Code! And I have 3
    other XSL-files also running perfect!
    And that thing I try is getting me the error!
    When I remove these xsl:choose/when statement everything works!
    And I only need the XPATH statement at this little point, because at
    this point I have to jump back!
    Till now I haven't to do that XPATH thing, but now!

    Please help me more!
     
    Kniffel, Sep 7, 2007
    #8
  9. Kniffel

    Kniffel Guest

    So ......:

    <xsl:variable name="PDTwert" select="/DLT/DisplayDrivingTime/
    attribute::*" />
    <xsl:choose>
    <xsl:when test="$PDTwert='True'">
    True
    </xsl:when>
    <xsl:when test="$PDTwert='False'">
    False
    </xsl:when>
    </xsl:choose>

    < >
    o o
    oooO.|.Oooo
     
    Kniffel, Sep 7, 2007
    #9
    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. Alastair Cameron
    Replies:
    1
    Views:
    7,522
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  2. Pat Turner
    Replies:
    4
    Views:
    580
    Pat Turner
    Dec 6, 2004
  3. Replies:
    1
    Views:
    3,687
    A. Bolmarcich
    May 27, 2005
  4. AR
    Replies:
    2
    Views:
    409
    William Park
    May 27, 2005
  5. HANM
    Replies:
    2
    Views:
    762
    Joseph Kesselman
    Jan 29, 2008
Loading...

Share This Page