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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:58:50+00:00 2026-06-03T04:58:50+00:00

I have a document with a specific attribute (myId) for which the value needs

  • 0

I have a document with a specific attribute (myId) for which the value needs to be updated whenever its value is zero. The document looks like this

<?xml version="1.0" encoding="UTF-8"?><Summary>
 <Section myId="0">
  <Section myId="0">
   <Para>...</Para>
  </Section>
  <Section myId="5">
   <Para>...</Para>
  </Section>
 </Section>
</Summary>

I am using a template to match the attribute myId in order to set it to a unique ID passed from a calling program but I only want to match one of the attributes in the document. Any additional attributes with a value of zero will be updated by passing a different ID.
My template I’m using looks like this:

 <xsl:template        match  = '@myId[.="0"]'>
  <xsl:attribute name = "{name()}">
   <xsl:value-of select = "$addValue"/>
  </xsl:attribute>
 </xsl:template>

The value addValue is a global parameter passed from the calling program.
I’ve searched for an answer for a good part of the day but I’m unable to have this template be applied only just once. The output replaces both myId values with the content of addValue.
I’ve tried to match with ‘@myId[.”0″][1]’ and I’ve tried to use the position() function to to match but my template is always applied to all myId attributes that are zero.

Is it possible to apply a matching template only once?

  • 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-03T04:58:52+00:00Added an answer on June 3, 2026 at 4:58 am

    Is it possible to apply a matching template only once?

    Yes:

    1. Whether a template is applied or not depends on the xsl:apply-templates that causes the template to be selected for execution.

    2. Additionaly, the match pattern can be specified in a way that guarantees that the template matches only one specific node in the document.

    Here is what you can do:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pNewIdValue" select="9999"/>
    
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
     "Section
       [@myId = 0
      and
        not((preceding::Section | ancestor::Section)
                     [@myId = 0]
           )
       ]/@myId">
      <xsl:attribute name="myId"><xsl:value-of select="$pNewIdValue"/></xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document:

    <Summary>
        <Section myId="0">
            <Section myId="0">
                <Para>...</Para>
            </Section>
            <Section myId="5">
                <Para>...</Para>
            </Section>
        </Section>
    </Summary>
    

    the wanted, correct result is produced:

    <Summary>
       <Section myId="9999">
          <Section myId="0">
             <Para>...</Para>
          </Section>
          <Section myId="5">
             <Para>...</Para>
          </Section>
       </Section>
    </Summary>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a document structured like this: { _id:43434, heroes : [ { nickname
I have a document, something like this: <root> <A node=1/> <B node=2/> <A node=3/>
I have a model like this: class Lesson include Mongoid::Document field :title, :type =>
I have a document library in MOSS 2007 which has folders and files. Document
I have XML exactly like this format: <?xml version=1.0 encoding=utf-8?> <book> <chapter> <verse>This is
I have implemented a program, to print the document to the specific printer using
How can I match all elements that have a specific attribute that is added
I have an XML document which I'm subjected to an XSLT. The structure is
A have the document with 23 figures and 46 tables (appendix to journal article).
I have a document and want to create it as a twocolumn-document. That works

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.