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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:09:55+00:00 2026-06-05T10:09:55+00:00

Given XML like: <a> <b> <c>some keyword</c> </b> </a> I need to add new

  • 0

Given XML like:

<a>
  <b>
    <c>some keyword</c>
  </b>
</a>

I need to add new node to the parent node of a in case if node c contains text “keyword”, so it will look like

<a>
  <b>
    <c>some keyword</c>
  </b>
</a>
<x> new node X </x>

I can match the text with expression:

<xsl:template match="//a/b/c[matches(text(),'\.*keyword\.*')]">
  <xsl:copy-of select="."/>
  <xsl:element name="x">
    <xsl:text> new node </xsl:text>
  </xsl:element>
</xsl:template>

and this results in

<a>
  <b>
    <c>some keyword</c>
    <x> new node X </x>
  </b>
</a>

How can I fix that?

  • 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-05T10:09:56+00:00Added an answer on June 5, 2026 at 10:09 am

    You have to add the x element while matching a ‘s parent. You can refer to any child element or attribute inside a predicate. What I mean is that you have to take a peek at what’s inside a’s parent while copying it. Doing it while copying the a, b or c element is just too late.

    The following stylesheet should do the trick. I don’t have any XSLT2.0 capable processor on me right now so I can’t check it but you should be able to see the logic in it.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:apply-templates ></xsl:apply-templates>
        </xsl:element>
    </xsl:template>
    <xsl:template match="//*[matches(a/b/c/text() , 'keyword')]">
        <xsl:element name="{name()}">
            <xsl:apply-templates ></xsl:apply-templates>
            <xsl:element name="x">
                <xsl:text> new node </xsl:text>
            </xsl:element>
        </xsl:element>
    </xsl:template>
    

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

Sidebar

Related Questions

given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
I have an XML file which contains some data as given. <?xml version=1.0 encoding=UTF-8
Hey I would like to find a given text in an xml that looks
Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
Given an XML structure like this: <garage> <car>Firebird</car> <car>Altima</car> <car>Prius</car> </garage> I want to
Given an xml structure like this <gesmes:Envelope> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</gesmes:name> </gesmes:Sender>
Given an XML element in jQuery like so: $('<foo oy=vey foo=bar here=is another=attribute />')
Given the following XML, I would like to return all eventtitles where the eventtype
i will give an example of the problem i have. My XML is like
Given the XML below, I need to group by InputItem and sum the costs

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.