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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:12:36+00:00 2026-05-31T05:12:36+00:00

I’m trying to change an xml by using regular expressions in java, but I

  • 0

I’m trying to change an xml by using regular expressions in java, but I can’t find the right way. I have an xml like this (simplified):

<ROOT>
   <NODE ord="1" />
   <NODE ord="3,2" />
</ROOT>

The xml actually shows a sentence with its nodes, chunks … in two languages and has more attributes. Each sentence it’s loaded in two RichTextAreas (one for the source sentence, and the other for the translated one).

What I need to do is add a style attribute to every node that has an specific value in its ord attribute (this style attribute will show correspondences between two languages, like Google Translate does when you mouse over a word). I know this could be done using DOM (getting all the NODE nodes and then seeing the ord attribute one by one), but I am looking for the fastest way to do the change as it is going to execute in the client side of my GWT app.

When that ord attribute has a single value (like in the first node) it is easy to do just taking the xml as a string and using the replaceAll() function . The problem is when the attribute has composed values (like in the second node).

For example, how could I do to add that attribute if the value I’m looking for is 2? I believe this could be done using regular expressions, but I can’t find out how. Any hint or help would be appreciated (even if it doesn’t use regexp and replaceAll function).

Thanks in advance.

  • 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-31T05:12:36+00:00Added an answer on May 31, 2026 at 5:12 am

    XPath can do this for you. You could select:

    /ROOT/NODE[contains(concat(',', @ord, ','), ',2,')]
    

    Since you intend to use GWT on the client, you could give gwtxslt a try. With it you could specify an XSLT stylesheet to do the transformation (i.e. adding the attribute) for you:

    XsltProcessor processor = new XsltProcessor();
    processor.importStyleSheet(styleSheetText);
    processor.importSource(sourceText);
    processor.setParameter("ord", "2");
    processor.setParameter("style", "whatever");
    String resultString = processor.transform();
    // do something with resultString
    

    where styleSheetText could be an XSLT document along the lines of

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:param name="ord"   select="''" />
      <xsl:param name="style" select="''" />
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="NODE">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:if test="contains(concat(',', @ord, ','), concat(',', $ord, ','))">
            <xsl:attribute name="style">
              <xsl:value-of select="$style" />
            </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="node()" />
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    Note that I use concat() to prevent partial matches in the comma-separated list that the attribute value of @ord actually is.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.