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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:06:21+00:00 2026-05-26T02:06:21+00:00

Could anyone please help me with the following transformation? Here is the input xml:

  • 0

Could anyone please help me with the following transformation?

Here is the input xml:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author>
        <name>Smith</name>
    </author>
    <author>
        <name>Wallace</name>
    </author>
    <author>
        <name>Brown</name>
    </author>
</book>
<book>
    <title>Other book</title>
    <pages>100</pages>
    <size>small</size>
    <author>King</author>
</book>
<book>
    <title>Pretty book</title>
    <pages>150</pages>
    <size>medium</size>
</book>

This is the desired output

<book style="even">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Smith</author-name>
</book>
<book style="odd">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Wallace</author-name>
</book>
<book style="even">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Brown</author-name>
</book>
<book style="odd" >
    <title>Other book</title>
    <pages>100</pages>
    <size>small</size>
    <author-name>King</author-name>
</book>
<book style="even">
    <title>Pretty book</title>
    <pages>150</pages>
    <size>medium</size>
    <author-name />
</book>

I tried using xsl:for-each loops, but I suppose they brought me to a dead end. The tricky part here is the “style” attribute that somehow needs to be “global” no matter how many author tags are placed in any book.

  • 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-26T02:06:22+00:00Added an answer on May 26, 2026 at 2:06 am

    This stylesheet first stores all author nodes as well as all book nodes without authors in a global variable. “/.” sorts them in document order. Then the main template iterates over all nodes in this variable and generates output according to the position in the sequence.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:variable name="authors" select="(//author | //book[not(author)])/."/>
    
      <xsl:template match="/">
        <xsl:element name="result">
          <xsl:for-each select="$authors">
            <xsl:apply-templates select=".">
              <xsl:with-param name="position" select="position()+1"/>
            </xsl:apply-templates>
          </xsl:for-each>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="text()|title|pages|size">
        <xsl:copy>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="book">
        <xsl:param name="position" select="1"/>
        <xsl:element name="book">
          <xsl:attribute name="style">
            <xsl:if test="$position mod 2 = 0">even</xsl:if>
            <xsl:if test="$position mod 2 = 1">odd</xsl:if>
          </xsl:attribute>
          <xsl:apply-templates select="title"/>
          <xsl:apply-templates select="pages"/>
          <xsl:apply-templates select="size"/>
          <xsl:element name="author-name"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="author">
        <xsl:param name="position" select="1"/>
        <xsl:element name="book">
          <xsl:attribute name="style">
            <xsl:if test="$position mod 2 = 0">even</xsl:if>
            <xsl:if test="$position mod 2 = 1">odd</xsl:if>
          </xsl:attribute>
          <xsl:apply-templates select="../title"/>
          <xsl:apply-templates select="../pages"/>
          <xsl:apply-templates select="../size"/>
          <xsl:element name="author-name">
            <xsl:if test="name">
              <xsl:value-of select="name"/>
            </xsl:if>
            <xsl:if test="not(name)">
              <xsl:value-of select="."/>
            </xsl:if>
          </xsl:element>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a WCF with following 3 tiers: Service BLL DLL. Could anyone please help
Ok so could anyone please help me out with the VB for auto entering
Could anyone please tell how jre directory help in the running java programs? I'm
Could anyone please tell me why the following line about filter init method invocation
Could anyone please tell me why the following casting is resulting in compile time
Could anyone please explain the following line of code, found on http://docs.openttd.org/ai__cargo_8cpp_source.html return (AICargo::TownEffect)::CargoSpec::Get(cargo_type)->town_effect;
Could anyone please help me with switch debouncing? I read Ganssle's & avr's articles
I want to convert the following PHP operation to C# code, could anyone please
Could anyone please help me to retreive the authenticated user who initiated a REST
I have an XML file like the following : <?xml version=1.0 encoding=ISO-8859-1 ?> <mylist

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.