Can’t get the actual value from xsl element

I had a problem today that was driving me quite crazy. Basically I tried to get the value from an xsl element (which has a couple of specific attributes)

Input XML

<meta name="dc.creator.personalname" content="John Smith"/>
<meta name="dc.creator.personalname.address" content="Dr"/>

Scenario 1

I wanted to get the value of personalname by itself if the personalname.address was “n/a”.

Scenario 2

I wanted to join personalname.address and personalname if there was a legit personalname.address value like “Dr”. Seems easy enough right? …errr nope

What I tried at first in the XSLT

<xsl:template match="meta[@name='dc.creator.personalname']">
 <xsl:variable name="the_name">
 <xsl:value-of select="@content"/>
 </xsl:variable>
 <xsl:variable name="the_address">
 <xsl:value-of select="//meta[@name='dc.creator.personalname.address'][@content]"/>
 </xsl:variable>
 <xsl:choose>
 <xsl:when test="$the_address !='n/a'">
 <dc:contributor.author xmlns:dc="http://purl.org/dc/elements/1.1/">
 <xsl:copy-of select="$the_address"/>
 <xsl:text> </xsl:text>
 <xsl:copy-of select="$the_name"/>
 </dc:contributor.author>
 </xsl:when>
 <xsl:otherwise>
 <dc:contributor.author xmlns:dc="http://purl.org/dc/elements/1.1/">
 <xsl:value-of select="$the_name"/>
 </dc:contributor.author>
 </xsl:otherwise>
 </xsl:choose>
 </xsl:template>
 

The important message

Take note of the line

<xsl:value-of select="//meta[@name='dc.creator.personalname.address'][@content]"/>

Going on all of the documentation that I have read this seems like an obvious solution to get the value of the content attribute, turns out this gave me the entire element instead, like this.

<meta name="dc.creator.personalname.address" content="Dr"/>

The solution was to use the following line in the XSLT

<xsl:value-of select="//meta[@name='dc.creator.personalname.address']/@content"/>

The difference is I am using closing square bracket, slash, at, content instead of closing square bracket, opening square bracket, at, content.
I tried 10 different ways to do this, thinking the problem was caused by the fact that I should have matched the value in a separate template and passed it in as a param/declared the value as a global variable or other crazy ideas… none of which worked.
Please let me know if there are any other ways around this but make sure you test them first before replying because as I know with XSLT nothing is what it seems.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s