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

  • Home
  • SEARCH
  • 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 722689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:01:10+00:00 2026-05-14T06:01:10+00:00

Is it possible for me to have a conditional in XSLT such that I

  • 0

Is it possible for me to have a conditional in XSLT such that I find and replace only the FIRST tag of a particular tag name?

For example, I have an XML file with many <title> tags. I would like to replace the first of these tags with <PageTitle>. The rest should be left alone. How would I do this in my transform? What I currently have is this:

<xsl:template match="title">
     <PageTitle>
       <xsl:apply-templates />
     </PageTitle>
</xsl:template>

which finds all <title> tags and replaces them with <PageTitle>. Any help would be greatly appreciated!

  • 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-14T06:01:10+00:00Added an answer on May 14, 2026 at 6:01 am

    The first title element in the document is selected by:

    (//title)[1]

    Many people mistakenly think that //title[1] selects the first title in the document and this is a frequently committed error. //title[1] selects every title element that is the first title child of its parent — not what is wanted here.

    Using this, the following transformation produces the required output:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "title[count(.|((//title)[1])) = 1]">
    
         <PageTitle>
           <xsl:apply-templates />
         </PageTitle>
     </xsl:template>
    </xsl:stylesheet>
    

    When applied on this XML document:

    <t>
     <a>
      <b>
        <title>Page Title</title>
      </b>
     </a>
     <b>
      <title/>
     </b>
     <c>
      <title/>
     </c>
    </t>
    

    the wanted result is produced:

    <t>
     <a>
      <b>
        <PageTitle>Page Title</PageTitle>
      </b>
     </a>
     <b>
      <title />
     </b>
     <c>
      <title />
     </c>
    </t>
    

    Do note how we use the well-known Kaysian method of set intersection in XPath 1.0:

    If there are two nodesets $ns1 and $ns2, the following expression selects every node which belongs to both $ns1 and $ns2:

    $ns1[count(.|$ns2) = count($ns2)]

    In the specific case when both node-sets contain only one node, and one of them is the current node, the following expression evaluates to true() exactly when the two nodes are identical:

    count(.|$ns2) = 1

    A variation of this is used in the match pattern of the template that overrides the identity rule:

    title[count(.|((//title)[1])) = 1]

    matches only the first title element in the document.

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

Sidebar

Related Questions

I have gleaned that it is possible to use conditional compiler directives and constants
Possible Duplicate: Conditional Check in Where clause I have a few parameters that I
Is it possible for Java foreach to have conditions? For example, for(Foo foo :
Is it possible have two projects with the same name in flex builder? Here
It is theoretically possible to have a converter, that scans the color areas of
Is it possible to have growing files on amazon s3? That is, can i
Is it possible to have a conditional installation configuration, slaved wth the Visual Studio
Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation
I am just wondering if it is possible to have a condition that passes
I have a table that defines the possible categories in my website - fields

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.