XML/XSL/XPATH how to get a element value

K

Kniffel

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
 
P

Pavel Lepin

Kniffel said:
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.
 
K

Kniffel

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?
 
M

Martin Honnen

Kniffel said:
<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
 
K

Kniffel

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...
 
P

Pavel Lepin

Kniffel said:
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.
 
K

Kniffel

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...
 
K

Kniffel

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.

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!
 
K

Kniffel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top