user6927689
user6927689

Reputation:

XSLT: Trouble concatenating words with an attribute

I am having trouble with pulling the YEAR attribute from my xml file and concatenating it with characters.

I am trying to get the xml to look like this:

<h1>CIA World 2008 Factbook - Countries</h1>

"2008" is apart of the YEAR attribute. Here is a piece of the xml document that I am using:

<WFB YEAR="2008">

WFB is the root element in the document.

So far I have this..

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="element[@YEAR]">
     <element>
      <h1><xsl:value-of select=
       "concat(CIA World ', @YEAR, ' Factbook - Countries)"/></h1>
     </element>
 </xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

I feel like I am over complicating it, though...

Upvotes: 0

Views: 41

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

Given the following input:

XML

<WFB YEAR="2008"/>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/WFB">
    <html>
        <h1>
            <xsl:value-of select="concat('CIA World ', @YEAR, ' Factbook - Countries')"/>
        </h1>
    </html>
</xsl:template>

</xsl:stylesheet>

will return:

<html>
   <h1>CIA World 2008 Factbook - Countries</h1>
</html>

Upvotes: 3

Related Questions