XSLT Multi Selection, XSLT Tutorial, Global Guideline

Forum | Blog


Basic XSLT Tutorial.     
 XSLT Introduction.          
 XSLT Templates.            
 XSLT Attributes.              
 XSLT Transformation.     
 XSLT Variables.              
 XSLT Formatting.            
 XSLT Loops.                   
 XSLT Sorting.                 
 XSLT Conditions.             
 XSLT Multi Selection.
 XSLT Functions.              
 XSLT Copying.                
 XSLT Combining.           
 XSLT Summary.              

XSLT Tutorial >> XSLT Multi Selection.

     Back             Next     

XSLT Multi Selection is use to convert multiple XML elements to a values for a single attribute. Here is the information about how this can be done with XSLT multi selection.

<!--XML structure for XSLT Multi Selection in XSLT Tutorial-->
  <Title>XSLT Tutorial at Global Guide Line</Title>
  <Keyword>XSLT Tutorial</Keyword>
  <Keyword>XSLT Function</Keyword>
  <Keyword>XSLT Transformation</Keyword>

From the above source elements <Keyword> should be converted to the below result.

<META name="keywords" content="XSLT Tutorial, XSLT Function, XSLT Transformation">

All above is done with this XSLT transformation

Converting Multiple Elements To One Attribute In XSLT Multi Selection.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--Exampleof XSLT Multi Selection in XSLT Tutorial-->
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
<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="WebPage/Head/Keyword">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">, </xsl:if>

<xsl:element> and <xsl:attribute> on above code allows dynamic creation of elements and attributes. <xsl:element name="META"> defines the element of <META>. and <xsl:attribute name="name"> keywords</xsl:attribute> results as <META name="keywords"> on output.  The content of the attribute content on above code is created dynamically with a selection of each <WebPage><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.

<xsl:template match="/">
    <META name="keywords">
      <xsl:attribute name="Content">
        <!--Applying template below this section of code-->
        <xsl:apply-templates select="WebPage/Head/Keyword" />

<xsl:template match="WebPage/Head/Keyword" >
  <!--Use of xsl-value in XSLT Multi Selection-->
  <xsl:value-of select="."/>
  <!--If condition use in XSLT Multi Selection Example-->
  <xsl:if test="following-sibling::Keyword">,</xsl:if>

     Back             Next     

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2021 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 520 visitors are online now