WSContractFirst - Version 0.3
New INETA .NET User Groups in Europe

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>



Feed You can follow this conversation by subscribing to the comment feed for this post.


how about lt;meta name=keywordsgt;lt;xsl:attribute name=Contentgt;lt;xsl:apply-templates select=Article/Head/Keyword mode=csv/gt;lt;/xsl:attributegt;lt;/metagt; then in the template for Keyword in the csv mode lt;xsl:value-of select=./gt;lt;xsl:if test=following-sibling::Keywordgt;,lt;/xsl:ifgt;


Yes, this is another option :-)

coach outlet online

I agree you the up.As it give me the same some feeling!

cheap juicy bag

allows dynamic creation of elements and attributes


clearance ugg boots for women ugg outlet


I had got a desire to begin my company, nevertheless I didn't earn enough amount of money to do it. Thank goodness my close fellow told to use the business loans. Therefore I used the financial loan and made real my desire.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)