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 6715157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:34:52+00:00 2026-05-26T08:34:52+00:00

I have following XML file – with a Book Name and respective Authors. The

  • 0

I have following XML file – with a Book Name and respective Authors. The Authors can be 1 or more in counts for the same Book –

<Authors>
 <book>
  <bName>HTML5</bName>
  <AName>John</AName>
  <AName>James</AName>
  <AName>Jack</AName>
 </book>
 <book>
  <bName>Java</bName>
  <AName>Joe</AName>  
 </book>
 <book>
  <bName>XML</bName>
  <AName>John</AName>
  <AName>James</AName>
 </book>
 ....
</Authors>

Using XSLT, how can I generate output as,

<p>
<b>HTML5</b/><br/>
John, James and Jack
</p>
<p>
<b>Java</b/><br/>
Joe
</p>
<p>
<b>XML</b/><br/>
John and James
</p>

In short, if there are more than 1 authors then separated by comma and an “and” between the last and last but one author name.

I am using XSLT 1.0. If not possible with 1.0, then can it be done with 2.0..?

Thanks in advance and have a nice day – John

  • 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-26T08:34:53+00:00Added an answer on May 26, 2026 at 8:34 am

    One way to do this is to have various matching templates for the AName elements.

    AName elements which must be followed by a comma must have 2 or more following AName elements:

    <xsl:template match="AName[following-sibling::AName[following-sibling::AName]]">
    

    Other AName elements which have a following AName element which are not picked up by the first match, must then need an “and” amd not a comma, after them.

    <xsl:template match="AName[following-sibling::AName]">
    

    All other AName elements with be the last in the list, and so have nothing following.

    So, given the following XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" indent="yes"/>
    
       <xsl:template match="Authors">
          <xsl:apply-templates select="@*|node()"/>
       </xsl:template>
    
       <xsl:template match="book">
          <p>
             <xsl:apply-templates select="@*|node()"/>
          </p>
       </xsl:template>
    
       <xsl:template match="bName">
          <b>
             <xsl:value-of select="."/>
          </b>
          <br/>
       </xsl:template>
    
       <xsl:template match="AName">
          <xsl:value-of select="."/>
       </xsl:template>
    
       <xsl:template match="AName[following-sibling::AName]">
          <xsl:value-of select="."/>
          <xsl:text> and </xsl:text>
       </xsl:template>
    
       <xsl:template match="AName[following-sibling::AName[following-sibling::AName]]">
          <xsl:value-of select="."/>
          <xsl:text>, </xsl:text>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to the following XML

    <Authors> 
     <book> 
      <bName>HTML5</bName> 
      <AName>John</AName> 
      <AName>James</AName> 
      <AName>Jack</AName> 
     </book> 
     <book> 
      <bName>Java</bName> 
      <AName>Joe</AName>   
     </book> 
     <book> 
      <bName>XML</bName> 
      <AName>John</AName> 
      <AName>James</AName> 
     </book> 
    </Authors> 
    

    The following HTML is output

    <p><b>HTML5</b><br>John, James and Jack</p>
    <p><b>Java</b><br>Joe</p>
    <p><b>XML</b><br>John and James</p>
    

    Do note the order of the matching templates in the XSLT is important. The most specific case has to come after the more general case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML File: <persons> <person name=shawn> <age>34</age> <hair style=spikes>red</hair> </person> <person
I have the following text in xml file: <Config Builder=LP Wizard> <Libraries> <Library Name=XCAMSource/>
I have a following XML file: <titles> <book title=XML Today author=David Perry/> <book title=XML
I have the following XML file: <book> <bookname child=test> <text> Works </text> <text> Doesn't
I have the following XML file <?xml version=1.0 ?> <Persons> <Person> <Id>1</Id> <Name>temp</Name> <Qlid>1234</Qlid>
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <scripts> <ScriptName> <name> My
I have the following XML file getting populated via list view. I can't get
I wonder whether someone can help me please. I have the following xml file
I have the following XML file: <?xml version=1.0 encoding=utf-8 ?> <strategies> <strategy name=God Class>
For example I have the following XML file: <?xml version=1.0 encoding=ISO-8859-1?> <bookstore> <book> <title

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.