Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get the value of an Attribute in XSL 2

Status
Not open for further replies.

Kirilla

Programmer
Jul 12, 2000
101
0
0
HU
Hi.

Could anybody help? I don't know, what XPath I should use to get the value of a node. Here are my XML and XSL source:

Contacts.xml
<?xml version=&quot;1.0&quot; ?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;alldata.xsl&quot;?>
<contacts>
<contact id=&quot;b&quot;>
<name>Nick</name>
<age>26</age>
<city>Budapest</city>
</contact>
<contact id=&quot;u&quot;>
<name>Peter</name>
<age>28</age>
<city>London</city>
</contact>
</contacts>

alldata.xsl
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<html>
<body>
<xsl:apply-templates select=&quot;//contact&quot;/>
</body>
</html>
</xsl:template>
<xsl:template match=&quot;contact&quot;>
<div>
<xsl:element name=&quot;??????&quot;>
<xsl:if match=&quot;.[@id='b']&quot;>
<xsl:attribute name=&quot;style&quot;>color:red</xsl:attribute>
</xsl:if>
<xsl:value-of select=&quot;name&quot;/><br/>
<xsl:value-of select=&quot;age&quot;/><br/>
<xsl:value-of select=&quot;city&quot;/>
</xsl:element>
</div>
</xsl:template>
</xsl:stylesheet>

Question: I'd like to put the value of id attribute of contact node as element( <b> or <u>). What XPath do I use?

regards, Kirilla
 
Hello, Kirilla,
I would suggest a workaround of your problem.
Use <span> element - it will not harm your HTML page but can be very useful in applying styles.
So your alldata.xsl will look like:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=&quot; <xsl:template match=&quot;/&quot;>
<html>
<body>
<xsl:apply-templates select=&quot;//contact&quot;/>
</body>
</html>
</xsl:template>
<xsl:template match=&quot;contact&quot;>
<div>
<xsl:element name=&quot;span&quot;>
<xsl:if match=&quot;.[@id='b']&quot;>
<xsl:attribute name=&quot;style&quot;>font-weight:bold</xsl:attribute>
</xsl:if>
<xsl:if match=&quot;.[@id='u']&quot;>
<xsl:attribute name=&quot;style&quot;>text-decoration:underline</xsl:attribute>
</xsl:if>
<xsl:value-of select=&quot;name&quot;/><br/>
<xsl:value-of select=&quot;age&quot;/><br/>
<xsl:value-of select=&quot;city&quot;/>
</xsl:element>
</div>
</xsl:template>
</xsl:stylesheet>

Hope this helps.
D.
 
Hi dianal.

Thanks a lot for your answer. I know, there are other ways to solve this problem, but I'd like to learn some of these.
I've read the w3school's tutorial and I saw this solution, but it didn't work:

<xsl:element name=&quot;{@id}&quot;>

As I know, brace is used to variables and param tags. I'd like to understand how it is working and why doesn't my solution work. <xsl:element name=&quot;{@id}&quot;>

The w3school address is here:

best regards, Kirilla
 
Hello, Kirilla,
The tricky part is with using old-dated naming space the result was something like <{id}> Nick ...
When using new naming space everything is working as expected.
<xsl:element name=&quot;{@id}&quot;> generates <b> Nick </b> and so on.
But you have to do server-side transforming (using ASP for example.)
In the w3schools example it was not shown which naming space was used.
I would recommend ZVON XSL Tutorial: Form main page there are also links to other tutorials and references.
Hope this helps.
D.
 
Hi dianal.

I also found the answer for my question. Thanks a lot for your answer. It was really helpfull. The WD-xsl namespace is supported by IE but the W3C recommendetion is XSL/Transform.

best regards, Kirilla
 
Hi,
in IE5.5 I get error
Unknown method. -->count()<--
when using
xsl:stylesheet xmlns:xsl=&quot;name space

but using
xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;only return an empty result

I am trying to count how many nodes i have and will output a non-blank space if 0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top