Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:17:44+00:00 2026-05-26T21:17:44+00:00

Following is the XML piece of code – <groups> <group i=1> <member t=P.M c=Y>ABC</member>

  • 0

Following is the XML piece of code –

<groups>
  <group i=1>
    <member t="P.M" c="Y">ABC</member>
    <member t="P.L">PQR</member>
    <member t="M">XYZ</member>
  </group>
  <group i=2>
    <member t="M" c="Y">ABC</member>
    <member t="M">PQR</member>  
  </group>
  <group i=3>
    <member t="P.L" c="Y">ABC</member>
    <member t="M">PQR</member>  
    <member t="M">XYZ</member>  
  </group>
  <group i=4>
    <member t="M">ABC</member>
    <member t="M" c="Y">PQR</member>    
  </group>
  <group i=5>
    <member t="M">ABC</member>
    <member t="M" c="Y">PQR</member>    
    <member t="M" c="Y">XYZ</member>    
  </group>
  <group i=6>
    <member t="M" dec="Y">ABC</member>
  </group>

</groups>

Desired HTML output using XSLT 1.0 –

<U>ABC</U>, P.M, PQR, P.L and XYZ, M
<U>ABC</U> and PQR, MM
<U>ABC</U>, P.L, PQR and XYZ, MM
ABC and <U>PQR</U>, MM
ABC, <U>PQR</U> and <U>XYZ</U>, MM
<U>ABC</U>, M

The partial XSLT solution for the above output is –

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' media-type='text/html'/>
<xsl:template match="/">
  <html>
  <body>
<xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="group">
    <p>
      <xsl:apply-templates select="@*|node()"/>
    </p>
</xsl:template>

<xsl:template match="member">
   <xsl:choose>
     <xsl:when test='@c = "Y"'>
       <u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
     </xsl:when>
     <xsl:otherwise>
       <b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="member[following-sibling::member]">
  <xsl:choose>
    <xsl:when test='@c = "Y"'>
      <u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
      <xsl:text> and </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
      <xsl:text> and </xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="member[following-sibling::member[following-sibling::member]]">
  <xsl:choose>
    <xsl:when test='@c = "Y"'>
      <u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
      <xsl:text>, </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
      <xsl:text>, </xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

The above XSLT is giving the output in correct format as required, but if the <member>
are of same type then how to add this type at the end..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:17:45+00:00Added an answer on May 26, 2026 at 9:17 pm

    In answer to your immediate question though, to output the correct number of t elements for a member, you could do something like this:

    <xsl:apply-templates select="../member/@t[. = current()/@t]"/>
    

    So, it will output a value for each matching @t attribute.

    However, I think you need to read up more on grouping, as Mr. Michael Kay suggested in your last question.

    In this case, you are grouping by a group attributeand a member attribute. So, you would define a key, like so

    <xsl:key name="members" match="member" use="concat(../@i, '|', @t)"/>
    

    Do note the use of the pipe | to concatenate the two parts of the key. You would need to pick a character that could not occur in either part of the key.

    Then, you can get the first element of each group like so

    <xsl:apply-templates 
     select="group/member[generate-id() = generate-id(key('members', concat(../@i, '|', @t))[1])]"
     mode="group"/>
    

    And to iterate over all the elements in the group, you can then do this…

    <xsl:apply-templates select="key('members', concat(../@i, '|', @t))"/>
    

    So, given the following XML

    <groups>
       <group i="1">
          <member t="P.M" c="Y">ABC</member>
          <member t="P.L">PQR</member>
          <member t="M">XYZ</member>
       </group>
       <group i="2">
          <member t="M" c="Y">ABC</member>
          <member t="M">PQR</member>
       </group>
       <group i="3">
          <member t="P.L" c="Y">ABC</member>
          <member t="M">PQR</member>
          <member t="M">XYZ</member>
       </group>
       <group i="4">
          <member t="M">ABC</member>
          <member t="M" c="Y">PQR</member>
       </group>
       <group i="5">
          <member t="M">ABC</member>
          <member t="M" c="Y">PQR</member>
          <member t="M" c="Y">XYZ</member>
       </group>
       <group i="6">
          <member t="M" c="Y">ABC</member>
       </group>
    </groups>
    

    Using the following XSLT….

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:key name="members" match="member" use="concat(../@i, '|', @t)"/>
    
       <xsl:template match="/groups">
          <xsl:apply-templates select="group/member[generate-id() = generate-id(key('members', concat(../@i, '|', @t))[1])]" mode="group"/>
       </xsl:template>
    
       <xsl:template match="member" mode="group">
          <xsl:apply-templates select="key('members', concat(../@i, '|', @t))"/>
          <xsl:text>, </xsl:text>
          <xsl:apply-templates select="../member/@t[. = current()/@t]"/>
          <xsl:variable name="others" select="count(following-sibling::member[@t != current()/@t])" />
          <xsl:choose>
             <xsl:when test="$others &gt; 1">, </xsl:when>
             <xsl:when test="$others = 1"> and </xsl:when>
             <xsl:otherwise>
                <xsl:text>&#13;</xsl:text>
             </xsl:otherwise>
          </xsl:choose>
       </xsl:template>
    
       <xsl:template match="member">
          <xsl:choose>
             <xsl:when test="@c='Y'">
                <u>
                   <xsl:value-of select="."/>
                </u>
             </xsl:when>
             <xsl:otherwise>
                <xsl:value-of select="."/>
             </xsl:otherwise>
          </xsl:choose>
          <xsl:variable name="others" select="count(following-sibling::member[@t = current()/@t])" />
          <xsl:choose>
             <xsl:when test="$others &gt; 1">, </xsl:when>
             <xsl:when test="$others = 1"> and </xsl:when>
          </xsl:choose>
       </xsl:template>
    </xsl:stylesheet>
    

    The following is output….

    <u>ABC</u>, P.M, PQR, P.L and XYZ, M
    <u>ABC</u> and PQR, MM
    <u>ABC</u>, P.L, PQR and XYZ, MM
    ABC and <u>PQR</u>, MM
    ABC, <u>PQR</u> and <u>XYZ</u>, MMM
    <u>ABC</u>, M
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following piece of code that reads through a given xml-file
Inside a class, I have the following piece of code: /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Errors, typeof(ErrorsType))]
I have following piece of code which tries to load an XML file from
I have the following piece of code to write data to an XML file.
I have the following piece of XML: <xml> <ObsCont xCampo=field1> <xTexto>example1</xTexto> </ObsCont> <ObsCont xCampo=field2>
Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?
Given the following XML: <Contact> <ContactID>41111-f15a-4fa1-b643-47877608f557</ContactID> <ContactStatus>ACTIVE</ContactStatus> <Name>ABC Ltd</Name> <EmailAddress>xxx@xxx.co</EmailAddress> <SkypeUserName>xxxdemo</SkypeUserName> <Addresses> <Address> <AddressType>STREET</AddressType>
The following piece of code worked fine if garbage collection was not turned on
I have the following piece of code in my XSLT file: <xsl:copy-of select=/root/Algemeen/foto/node() />
I have the following piece of code: try{ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.