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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:30:03+00:00 2026-05-12T15:30:03+00:00

I have a quite stupid question. How can I make sure that my XML

  • 0

I have a quite stupid question. How can I make sure that my XML mixed content node doesn’t get mixed up? I have, say, an XML structure resembling this.

<root>
 <book>
  <title>Stuff</title>
  <description> This book is <i>great</i> if you need to know about stuff.
                I suggest <link ref="Things">this one</link> if you need to know
                about things. </description>
 </book>
 [other books]
</root> 

I need the final content to look like this

<h1>List of books</h1>
<h2><a name="Stuff"/>Stuff</h2>
<p> This book is <i>great</i> if you need to know about stuff.
    I suggest <a href="#Things">this one</a> if you need to know
    about things. </p>

But I can’t extract the portions of the text node, I always grab the whole thing. I’m using the descendant axis. Any clue what I’m doing wrong?

Here is my xslt:

<xsl:template match="description/*">
    <xsl:for-each select="following-sibling::*">
            <xsl:choose>
            <xsl:when test="name(.)='link'">
                <a href="{@ref}"><xsl:value-of select="."/></a>
            </xsl:when>
            <xsl:when test="name(.)='em'">
                <em><xsl:value-of select="."/></em>
            </xsl:when>
            <xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>    
        </xsl:choose>
    </xsl:for-each>
  </xsl:template>

Please note that the enclosed XML and resulting html are merely examples, I have to deal with a bigger structure which I’m not enclosing in, for sake of clarity.

  • 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-12T15:30:04+00:00Added an answer on May 12, 2026 at 3:30 pm

    <xsl:apply-templates> is your friend:

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output method="html" />
    
      <xsl:template match="root">
        <h1>List of books</h1>
        <xsl:apply-templates />
      </xsl:template>
    
      <!-- a <book> consists of its <title> and <description> -->
      <xsl:template match="book">
        <xsl:apply-templates select="title" />
        <xsl:apply-templates select="description" />
      </xsl:template>
    
      <!-- <title> is turned into a <h2> -->
      <xsl:template match="title">
        <h2>
          <a name="{.}"/>
          <xsl:value-of select="." />
        </h2>
      </xsl:template>
    
      <!-- <description> is turned into a <p> -->
      <xsl:template match="description">
        <p>
          <xsl:apply-templates />
        </p>
      </xsl:template>
    
      <!-- default rule: copy any node beneath <description> -->
      <xsl:template match="description//*">
        <xsl:copy>
          <xsl:copy-of select="@*" />
          <xsl:apply-templates />
        </xsl:copy>
      </xsl:template>
    
      <!-- override rule: <link> nodes get special treatment -->
      <xsl:template match="description//link">
        <a href="#{@ref}">
          <xsl:apply-templates />
        </a>
      </xsl:template>
    
      <!-- default rule: ignore any unspecific text node -->
      <xsl:template match="text()" />
    
      <!-- override rule: copy any text node beneath description -->
      <xsl:template match="description//text()">
        <xsl:copy-of select="." />
      </xsl:template>
    
    </xsl:stylesheet>
    

    The following output is generated for your input XML (Note: I piped it through tidy for the sake of readability. Non-relevant white-space was removed in the process):

    <h1>List of books</h1>
    <h2><a name="Stuff">Stuff</h2>
    <p>This book is <i>great</i> if you need to know about stuff. I
    suggest <a href="#Things">this one</a> if you need to know about
    things.</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey folks, I have a question which feels stupid but I can't quite say
I hope this is not a completely stupid question. I have searched quite a
I'm quite sure this is a stupid question, but all my tries failed. I
This question should be quite straightforward, maybe stupid but I just can't find the
I have quite a few RewriteRules in my .htaccess that looks like this RewriteRule
I have quite big document in html format that generated from Microsoft Word. It
I know that might sound like a stupid question since it's a trivial feature
I know this is a stupid question, but I have been teaching myself c#,
I have a simple, general question regarding a real small issue that bothers me:
I know this question might sound quite stupid, but I've tried my best and

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.