7 Replies - 810 Views - Last Post: 26 April 2012 - 08:35 AM

#1 Machina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Using XSL Attributes

Posted 25 April 2012 - 05:26 PM

I hope this is the right place for this, I apologize if it isn't.

I'm learning XSL right now and am attempting to utilize HTML and CSS to create a webpage. All told, it's a fairly simple assignment but I'm not sure how to make use of attributes.

Say I have a faculty member with a firstName, lastName, email, and two phone numbers, mobile and office. So the ones throwing me are the phone numbers. The XML code for them is:

<phone type="mobile">123-456-7890</phone>
<phone type="office">098-765-4321</phone>


I'm displaying this content in a table, it's simplest and the idea is just to learn how to make use of XSL. The thing is, I'm not sure how to get to those two different phone numbers. I've tried running searches, I've checked our in-class examples, and I've tinkered around with the code myself. I can't figure out what to do. So I currently have a table set up and it's displaying everything that doesn't have an attribute - names, email, etc. - but not those numbers.

Full code at present is:

<table border="1">
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Office Phone</th>
                        <th>Mobile Phone</th>
                        <th>Office</th>
                    </tr>
                <xsl:for-each select="//faculty">
                        <tr>
                            <td><xsl:value-of select="./firstName"/></td>
                            <td><xsl:value-of select="./lastName"/></td>
                            <td><xsl:value-of select="./email"/></td>
                            <td><xsl:value-of select="./@phone" /></td>
                            <td><xsl:value-of select="@phone" /></td>
                            <td>
                                <xsl:value-of select="./officeLocation/building" />
                                -
                                <xsl:value-of select="./officeLocation/room" />
                            </td>
                        </tr>
                </xsl:for-each> 
</table>




It's probably not 100% right or even efficient at this point but I'm going to play with that once I figure this attribute thing out. Neither @phone works. Any idea how I can do this so I can have a column for mobile numbers and a column for office numbers? I have other things I'd like to do with attributes (calling certain parts of the directory like schools and colleges) but I can't very well do that until I see if I can do this so if anyone can give me a hand I'd appreciate it!


Thanks!

This post has been edited by Dormilich: 25 April 2012 - 11:19 PM
Reason for edit:: edited per request


Is This A Good Question/Topic? 0
  • +

Replies To: Using XSL Attributes

#2 Machina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Re: Using XSL Attributes

Posted 25 April 2012 - 05:44 PM

If I try:

<td><xsl:value-of select="@phone='mobile'" /></td>



It prints out "false"... =/
Was This Post Helpful? 0
  • +
  • -

#3 Machina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Re: Using XSL Attributes

Posted 25 April 2012 - 05:51 PM

Also, in the first post one was meant to be mobile, the other office. I don't see an Edit Your Post option, sorry. =/
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4245
  • View blog
  • Posts: 13,452
  • Joined: 08-June 10

Re: Using XSL Attributes

Posted 25 April 2012 - 11:18 PM

with the XML as given, it should be <xsl:value-of select="./phone"/>, since you have a <phone> tag. @ is shorthand for attribute:: (which is rather self-explaining).

to also select a type, youd use <xsl:value-of select="./phone[@type='mobile']"/> (though Im not sure how XSL handles it when you get more than one match).
Was This Post Helpful? 1
  • +
  • -

#5 Machina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Re: Using XSL Attributes

Posted 26 April 2012 - 04:13 AM

View PostDormilich, on 25 April 2012 - 11:18 PM, said:

to also select a type, youd use <xsl:value-of select="./phone[@type='mobile']"/> (though Im not sure how XSL handles it when you get more than one match).


That was exactly what I needed, thank you so much!

The types are different, I just made a mistake in my OP and didn't see an edit button. :)
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4245
  • View blog
  • Posts: 13,452
  • Joined: 08-June 10

Re: Using XSL Attributes

Posted 26 April 2012 - 05:18 AM

View PostMachina, on 26 April 2012 - 01:13 PM, said:

The types are different, I just made a mistake in my OP and didn't see an edit button. :)

IIRC the time frame where you can edit your post depends on your post count (and status).


PS. one of my favourite XPath expressions:
<xsl:for-each select="kbl:subsection/kbl:seite[@collection][count(.|key('colly', @collection)[1]) = 1]">
    <xsl:call-template name="collie"/>
</xsl:for-each>


This post has been edited by Dormilich: 26 April 2012 - 05:21 AM

Was This Post Helpful? 1
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: Using XSL Attributes

Posted 26 April 2012 - 06:13 AM

Just a quick note. Try to avoid for-each if at all possible. XSLT is declarative and working with that makes for far less nesting. e.g.
<table border="1">
	<tr>
		<th>First Name</th>
		<th>Last Name</th>
		<th>Email</th>
		<th>Office Phone</th>
		<th>Mobile Phone</th>
		<th>Office</th>
	</tr>
	<xsl:apply-templates select="//faculty" />
</table>

...

<xsl:template match="faculty">
    <tr>
        <td><xsl:value-of select="firstName"/></td>
        <td><xsl:value-of select="lastName"/></td>
        <td><xsl:value-of select="email"/></td>
        <td><xsl:value-of select="phone[@type='office']" /></td>
        <td><xsl:value-of select="phone[@type='mobile']" /></td>
        <xsl:apply-templates select="officeLocation" />
    </tr>
</xsl:template>

<xsl:template match="faculty/officeLocation">
	<td>
		<xsl:value-of select="building" />
		-
		<xsl:value-of select="room" />
	</td>
</xsl:template>


Was This Post Helpful? 1
  • +
  • -

#8 Machina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-April 12

Re: Using XSL Attributes

Posted 26 April 2012 - 08:35 AM

Well, we're just now learning XSL and for-each was okay to use for this week's assignment but he covered more today so now we're moving to templates instead. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1