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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:10:45+00:00 2026-05-26T00:10:45+00:00

I have an XML document that I want to generate unique IDs for. Some

  • 0

I have an XML document that I want to generate unique IDs for. Some nodes may already have the attribute in which case this is to be replaced. I want all nodes in the document to have the attribute.

An example document would be

<root>
<anode uid='123'/>
<anode/>
</root>

I am using the following stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:attribute name="uid">
                <xsl:value-of select="generate-id(.)"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

And am getting the following output. This is close to what I want, but how do I prevent the existing ID from being created as a text node?

<root uid="id515559">
<anode uid="id515560">123</anode>
<anode uid="id515562"/>
</root>

I have looked at XSLT: How to change an attribute value during <xsl:copy>? but I couldn’t get this to create new attributes.

If it makes a difference I’m using lxml to process the stylesheet.

  • 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-26T00:10:46+00:00Added an answer on May 26, 2026 at 12:10 am

    The Built-in template rule is being applied to the input @uid by the line:

    <xsl:apply-templates select="@*|node()"/>
    

    The Default template for an attribute copies the text, behaving as though you had defined a template thus:

    <xsl:template match="text()|@*">
      <xsl:value-of select="."/>
    </xsl:template>
    

    In order to prevent this being applied, either change your apply-templates select attribute so that it does not apply to attributes, or define a new blank template for any attribute, thus:

    <xsl:template match="@*" />
    

    You could even be more specific, using a similar template to ignore uid attributes only, thus:

    <xsl:template match="@uid" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that I generate from an Entity Framework object. The
I have a reasonably complex XML document which I want to flatten down to
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
I have some working Javascript code that generates an RDF/XML document using variables picked
HI there, I have an xml that is dynamically generated, from which I want
I have an xml document. When parsing that doc I want to do <c:set
I have a collection that I want to serialize to an xml document. The
I have an XML document that have additonal nodes in it at different levels
I have an XML documment that I want to load for the iPhone, do
I have an XML document that I'm trying to style via CSS. A relevant

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.