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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:45:07+00:00 2026-06-15T16:45:07+00:00

I have a xsl template which can replace some xml values. Now I want

  • 0

I have a xsl template which can replace some xml values.
Now I want these values to be dynamically generated by my code.

Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("foo.xsl"));
trans.transform(new StreamSource(new File("foo.xml"), new StreamResult(new File("output.xml")));

How can I acchive eg replacing the name ONLY where id=1? And moreover provide that id dynamically by javacode, not hardcoded?

<?xml version="1.0"?>
<my:accounts xmlns:my="http://myns">
<my:account>
<my:name>alex</my:name>
<my:id>1</my:id>
</my:account>
<my:account>
<my:name>Fiona</my:name>
<my:id>2</my:id>
</my:account>
</my:accounts>

This replaces all names:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://myns">

 <xsl:param name="propertyName" select="'alex'"/>

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

<xsl:template match="*[local-name()='account']/*[local-name()='name']/text()[.='{$propertyName}']">
       <xsl:text>johndoe</xsl:text>
      </xsl:template>
    </xsl: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-06-15T16:45:08+00:00Added an answer on June 15, 2026 at 4:45 pm

    This transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="http://myns">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pId" select="2"/>
    
      <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
     </xsl:template>
    
     <xsl:template match="my:name/text()">
       <xsl:choose>
         <xsl:when test="../../my:id = $pId">johndoe</xsl:when>
         <xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise>
       </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <my:accounts xmlns:my="http://myns">
        <my:account>
            <my:name>alex</my:name>
            <my:id>1</my:id>
        </my:account>
        <my:account>
            <my:name>Fiona</my:name>
            <my:id>2</my:id>
        </my:account>
    </my:accounts>
    

    produces the wanted, correct result:

    <my:accounts xmlns:my="http://myns">
       <my:account>
          <my:name>alex</my:name>
          <my:id>1</my:id>
       </my:account>
       <my:account>
          <my:name>johndoe</my:name>
          <my:id>2</my:id>
       </my:account>
    </my:accounts>
    

    Explanation:

    1. Use of a global-scope <xsl:param>. Although a default value is set, this is overriden by the value specified by the invoker of the transformation.

    2. Do note that the question how to specify the value of an external parameter to the transformation has different answers for differen xslt processors (vendors). You need to read the documentation of the specific XSLT processor you are using to get this answer for your particular case.

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

Sidebar

Related Questions

I have a variable from which I need to dynamically generate nodes <xsl:template match=banner_discount_1
I have an XML file which is transformed with XSL. Some elements have to
Suppose i have a template foo which can output something given a parameter. Now
I have a xml that looks like below <xsl:template match=//xml> <xsl:for-each select=//z:row> <ul class
I have an XSL style sheet for which I need to add some custom
I have some XML files of this form: <?xml version=1.0 encoding=utf-8?> <?xml-stylesheet type=text/xsl href=biomirror.xsl?>
In my XSL i have included a dropdown box which have values like, one
I have an XML to which i need to apply a XSL to transform
I have some code which is throwing an XslTransformException. This is the desired behavior
I have a docBook 4.4 XML file which is a user guide. I can

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.