0

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

1 Answers1

3

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>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thank you, Michael.hor257k! I just tried out your example code, but for some reason when I transform my XML, my html file shows up empty. Any advice? –  Nov 21 '16 at 20:35
  • 1
    This code will generate the shown HTML given the shown XML. If you're having trouble with it, you'll have to do a much better job of **relaying exactly how you're running the code**. If you're trying to run XSLT in the browser, see [**XSLT not working in web browser**](http://stackoverflow.com/questions/29941662/xslt-not-working-in-web-browser). – kjhughes Nov 21 '16 at 21:19