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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:14:20+00:00 2026-06-03T14:14:20+00:00

Below is my XML code – <Para> <Desc>….</Desc> <References> <BookRef> <BookName>ABC of HTML</BookName> <Chapter>1</Chapter>

  • 0

Below is my XML code –

<Para>
  <Desc>....</Desc>
  <References>
    <BookRef>
      <BookName>ABC of HTML</BookName>
      <Chapter>1</Chapter>
      <BookName>HTML : The Complete Reference</BookName>
      <Chapter>1</Chapter>
    </BookRef>
  </References>
</Para>
<Para>
  <Desc>....</Desc>
  <References>
    <BookRef>
      <BookName>ABC of XML</BookName>
      <Chapter>2</Chapter>
      <BookName>XML : The Complete Reference</BookName>
      <Chapter>10</Chapter><Chapter>11</Chapter>
    </BookRef>
  </References>
</Para>

I need to display the above in tabular format using HTML Table Tag. So that it should look like –

Description of Paragraph (the text between the Desc tags)

**Book Name**                                  **Chapters**
ABC of HTML                                      1
HTML: The Complete Reference                     1

Description of Paragraph (the text between the Desc tags)

**Book Name**                                  **Chapters**
ABC of XML                                       2
HTML: The Complete Reference                     10, 11

The reader can directly jump on the said chapters, I have created hyperlinks.
Below is the XSLT code –

<xsl:template match="References">
   <xsl:if test="CaseRef != ''"><br/>
      <table border="1" width="100%">
      <tr>
        <td width="75%">Book Name</td>
        <td align="right">Chapters</td>
      </tr>
      <xsl:for-each select="BookName">
      <tr>
        <td valign="top">
          <xsl:value-of select="."/>
        </td>
        <td align="right" valign="bottom">
          <xsl:for-each select="following::Chapter">
          <a id="lnk">
         <!-- This code will create a hyperlink to jump directly on the said chapter-->
            <xsl:attribute name="href">
              <xsl:value-of select="concat(concat('#',.),./@L)"/>
            </xsl:attribute>
            <xsl:value-of select="."/><xsl:text> </xsl:text>
          </a>
          </xsl:for-each>
        </td>
      </tr>
      </xsl:for-each>
  </table>
   </xsl:if>
</xsl:template>

I am missing something (might be much more) to get the required output!!

  • 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-06-03T14:14:22+00:00Added an answer on June 3, 2026 at 2:14 pm

    There are couple of problems with your XSLT. Firstly, within the References template you are looping over BookName name elements, but these are nested within BookRef elements, so you should be doing

    <xsl:for-each select="BookRef/BookName" />
    

    (Or preferably, use apply-templates to avoid too much nested code)

    The next issue is with this loop, where you loop over the chapters

    <xsl:for-each select="following::Chapter"> 
    

    The problem here is that will pick up all following Chapter elements, even those that occur after a following Bookname element. One way to fix this is define a keep, to look-up only those Chapter elements for a given book.

    <xsl:key 
       name="Chapters" 
       match="Chapter" 
       use="generate-id(preceding-sibling::BookName[1])"/>
    

    Then, assuming you are positioned on a BookName element, you can get the matching Chapter elements, like so:

    <xsl:apply-templates select="key('Chapters', generate-id())"/>
    

    Try the following XSLT (Note I have removed the reference to CaseRef that occurred in your original XSLT)

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
       <xsl:key name="Chapters" match="Chapter" use="generate-id(preceding-sibling::BookName[1])"/>
    
       <xsl:template match="Desc">
          <p>
             <xsl:value-of select="."/>
          </p>
       </xsl:template>
    
       <xsl:template match="References">
          <br/>
          <table border="1" width="100%">
             <tr>
                <td width="75%">Book Name</td>
                <td align="right">Chapters</td>
             </tr>
             <xsl:apply-templates select="BookRef/BookName"/>
          </table>
       </xsl:template>
    
       <xsl:template match="BookName">
          <tr>
             <td valign="top">
                <xsl:value-of select="."/>
             </td>
             <td align="right" valign="bottom">
                <xsl:apply-templates select="key('Chapters', generate-id())"/>
             </td>
          </tr>
       </xsl:template>
    
       <xsl:template match="Chapter">
          <xsl:if test="position() &gt; 1">
             <xsl:text>,</xsl:text>
          </xsl:if>
          <a id="lnk"><!-- This code will create a hyperlink to jump directly on the said chapter-->
             <xsl:attribute name="href">
                <xsl:value-of select="concat(concat('#',.),./@L)"/>
             </xsl:attribute>
             <xsl:value-of select="."/>
             <xsl:text/>
          </a>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML (assuming it has a single root element), the following is output

    <p>....</p>
    <br/>
    <table border="1" width="100%">
       <tr>
          <td width="75%">Book Name</td>
          <td align="right">Chapters</td>
       </tr>
       <tr>
          <td valign="top">ABC of HTML</td>
          <td align="right" valign="bottom">
             <a id="lnk" href="#1">1</a>
          </td>
       </tr>
       <tr>
          <td valign="top">HTML : The Complete Reference</td>
          <td align="right" valign="bottom">
             <a id="lnk" href="#1">1</a>
          </td>
       </tr>
    </table>
    <p>....</p>
    <br/>
    <table border="1" width="100%">
       <tr>
          <td width="75%">Book Name</td>
          <td align="right">Chapters</td>
       </tr>
       <tr>
          <td valign="top">ABC of XML</td>
          <td align="right" valign="bottom">
             <a id="lnk" href="#2">2</a>
          </td>
       </tr>
       <tr>
          <td valign="top">XML : The Complete Reference</td>
          <td align="right" valign="bottom">
             <a id="lnk" href="#10">10</a>,
             <a id="lnk" href="#11">11</a></td>
       </tr>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with layout like in below xml code <?xml version=1.0 encoding=utf-8?> <LinearLayout
I'm trying to update an element in the XML document below: Here's the code:
The code below correctly returns the XML from the soap sever that I'm accessing.
I have code as below: var s : String = hello world var xml
I tried accessing web for xml parsing using the code below: System.Uri proxy =
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
Please find the below code <?xml version=1.0 encoding=utf-8?> <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout=absolute> <mx:Script> <![CDATA[ [Bindable]
I have this below code and it work fine header (content-type: text/xml); $xml =
C# 2008 SP1 I am using the code below: dt.ReadXml(%AppData%\\DateLinks.xml); However, I am getting
I have the below xml's in my code XML Parsing Error: not well-formed Location:

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.