XSLT: Converting multiple elements to one attribute

I've received a question how to convert multiple XML elements to a values for a single attribute. Here is the information about how this can be done with XSLT.

The XML source:

  <Title>.NET Enterprise Services</Title>
  <Keyword>Enterprise Services</Keyword>

From this source the elements <Keyword> should be converted to this result:

<META name="keywords" content="Framework, C#, Enterprise Services">

This is done with this XSLT transformation:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Demo Style sheet to convert multiple elements to a single attribute value 
<xsl:stylesheet xmlns:xsl="
 <xsl:output method="html" indent="yes"/>
 <xsl:template match="/">
    <xsl:element name="META">
     <xsl:attribute name="name">keywords</xsl:attribute>
     <xsl:attribute name="content">
      <xsl:for-each select="Article/Head/Keyword">
       <xsl:value-of select="."/>
       <xsl:if test="position() != last()">, </xsl:if>

<xsl:element> and <xsl:attribute> allows dynamic creation of elements and attributes. <xsl:element name="META"> defines the element <META>. <xsl:attribute name="name">keywords</xsl:attribute> results in <META name="keywords">. The content of the attribute content is created dynamically with a selection of each <Article><Head><Keyword> element, and in between of these values a semicolon is added. <xsl:if test=position() != last()"> checks if the <Keyword> element was not the last one, as with the last one no more semicolon is used.

I really like the XSLT editor and debugger of Visual Studio 2005. This makes it easy to create XSLT files.

Update: Bryan has another option to create the Meta tags:

 <xsl:template match="/">
    <META name="keywords">
     <xsl:attribute name="Content">
      <xsl:apply-templates select="Article/Head/Keyword" />
 <xsl:template match="Article/Head/Keyword" >
  <xsl:value-of select="."/>
  <xsl:if test="following-sibling::Keyword">,</xsl:if>



  • how about

    &amp;lt;meta name=&quot;keywords&quot;&amp;gt;&amp;lt;xsl:attribute name=&quot;Content&quot;&amp;gt;&amp;lt;xsl:apply-templates select=&quot;Article/Head/Keyword&quot; mode=&quot;csv&quot;/&amp;gt;&amp;lt;/xsl:attribute&amp;gt;&amp;lt;/meta&amp;gt;

    then in the template for Keyword in the csv mode

    &amp;lt;xsl:value-of select=&quot;.&quot;/&amp;gt;&amp;lt;xsl:if test=&quot;following-sibling::Keyword&quot;&amp;gt;,&amp;lt;/xsl:if&amp;gt;

  • Yes, this is another option :-)

  • This is exactly what I was looking! Thanks a lot.

  • I need help to get a xsl format for the below xml code
    note:- path or file name is dynamic

    pls help me.





  • I don't create many responses, but i did some searching and wound up here XSLT: Converting multiple elements to one attribute - Christian Nagel's OneNotes.

    And I actually do have a couple of questions for you if you do not mind.
    Could it be only me or does it look like some of the responses look
    like they are coming from brain dead individuals? :-P And, if you are posting at other
    social sites, I'd like to follow anything new you have to post. Would you list of all of your social pages like your twitter feed, Facebook page or linkedin profile?

Comments have been disabled for this content.