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:

<Article>
 <Head>
  <Title>.NET Enterprise Services</Title>
  <MainCategory></MainCategory>
  <Keyword>Framework</Keyword>
  <Keyword>C#</Keyword>
  <Keyword>Enterprise Services</Keyword>
 </Head>
 <Body>
 </Body>
 <Info>
 </Info>
</Article>

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="
http://www.w3.org/1999/XSL/Transform"
    version="1.0">
 <xsl:output method="html" indent="yes"/>
 <xsl:template match="/">
  <HTML>
   <HEAD>
    <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:for-each>
     </xsl:attribute>
    </xsl:element>
    <TITLE>
    </TITLE>
   </HEAD>
   <BODY></BODY>
  </HTML>
 </xsl:template>
</xsl:stylesheet>

<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="/">
  <HTML>
   <HEAD> 
    <META name="keywords">
     <xsl:attribute name="Content">
      <xsl:apply-templates select="Article/Head/Keyword" />
     </xsl:attribute>
    </META>
    <TITLE>
    </TITLE>
   </HEAD>
   <BODY></BODY>
  </HTML>
 </xsl:template>
 
 <xsl:template match="Article/Head/Keyword" >
  <xsl:value-of select="."/>
  <xsl:if test="following-sibling::Keyword">,</xsl:if>
 </xsl:template>

Christian

Comments

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

bryan

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;

Christian

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

salescheapuggboots

clearance ugg boots for women salescheapuggboots.com ugg outlet

JasmineLandry32

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.

The comments to this entry are closed.