473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Replace all instances of an element

Hi, say I have the following XHTML document:

<div id="Fred">
<div id="Bert">
<div id="Jim">
<p>Hello</p>
<p>etc</p>
</div>
<div id="Bob">
<p>Goodbye</p>
<img src="arg.gif" />
</div>
</div>
</div>

Say I want to replace all <div> elements with <fred> elements, so the output
would look like this:

<fred id="Fred">
<fred id="Bert">
<fred id="Jim">
<p>Hello</p>
<p>etc</p>
</fred>
<fred id="Bob">
<p>Goodbye</p>
<img src="arg.gif" />
</fred>
</fred>
</fred>

Would I use XSLT?, and programmatically create an XSLT string, and use
XslTransform.Load() and XslTransform.Transform()?

If so, how do I specify in XSLT to replace all the <div> tags with <fred>
tags, and keep all the other tags the same?
Apr 15 '06 #1
7 1389


Yourself wrote:
I have the following XHTML document:

<div id="Fred">
<div id="Bert">
<div id="Jim">
<p>Hello</p>
<p>etc</p>
</div>
<div id="Bob">
<p>Goodbye</p>
<img src="arg.gif" />
</div>
</div>
</div>

Say I want to replace all <div> elements with <fred> elements, so the output
would look like this:

<fred id="Fred">
<fred id="Bert">
<fred id="Jim">
<p>Hello</p>
<p>etc</p>
</fred>
<fred id="Bob">
<p>Goodbye</p>
<img src="arg.gif" />
</fred>
</fred>
</fred>

Would I use XSLT?,
You can use an XSLT 1.0 stylesheet like this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" />

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

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

</xsl:stylesheet>

and programmatically create an XSLT string, and use
XslTransform.Load() and XslTransform.Transform()?


You can put that stylesheet into a file and Load from there, then cache
the XslTransform instance and call Transform whenever you need to.
Keeping the XSLT stylesheet in a string and Load from there is of course
also possible with the various overloads the Load method has.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 15 '06 #2
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

<snip>

Excellent, thank you very much!
Apr 15 '06 #3

"Yourself" <in*****@email.address> wrote in message
news:4a******************@text.news.blueyonder.co. uk...
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

<snip>

Excellent, thank you very much!


How do I replace an element with a prefix? i.e. I have:

<fred:text />

and I want to replace it with <span>

I've tried:

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

But that doesn't work :(
Apr 16 '06 #4


Yourself wrote:

How do I replace an element with a prefix? i.e. I have:

<fred:text />
In the source XML there should be a declaration alike
<fred:text xmlns:fred="http://example.com/whatever">

Do the same in the stylesheet e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fred="http://example.com/whatever"
version="1.0">

then this attempt
I've tried:

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


should do.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 16 '06 #5

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...


Yourself wrote:

How do I replace an element with a prefix? i.e. I have:

<fred:text />


In the source XML there should be a declaration alike
<fred:text xmlns:fred="http://example.com/whatever">

Do the same in the stylesheet e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fred="http://example.com/whatever"
version="1.0">

then this attempt
I've tried:

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


should do.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


thanks again!
Apr 16 '06 #6
<snip>

Ok, last one - how do you match an element that has a specific ID?

So, I want to match this:

<fred:text id="Bert" />

I've tried

<xsl:template match="/fred:text[@id="Bert"]">
<span>hello</span>
</xsl:template>

It doesn't like the quotations, and says the xml is not well formed.
I've tried &quot; as well, but I think it treats that as a word, not an
entity reference.
Apr 16 '06 #7

"Yourself" <in*****@email.address> wrote in message
news:nN*****************@text.news.blueyonder.co.u k...
<snip>

Ok, last one - how do you match an element that has a specific ID?

So, I want to match this:

<fred:text id="Bert" />

I've tried

<xsl:template match="/fred:text[@id="Bert"]">
<span>hello</span>
</xsl:template>

It doesn't like the quotations, and says the xml is not well formed.
I've tried &quot; as well, but I think it treats that as a word, not an
entity reference.


Ah, I got it, use single quotations:

<xsl:template match="/fred:text[@id='Bert']">
<span>hello</span>
</xsl:template>
Apr 16 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
3
by: gregpinero | last post by:
I'm trying to write a little script that will have a list of word pairs which will loop through that list and replace all instances of each word with the other word. I'm very new to javascript...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
0
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how...
8
by: VK | last post by:
Can be multiple instances of element used as the root element? That's a curly way of asking, but I did not come up with a better sentence, sorry. What I mean is with a document like: <?xml...
17
by: alxasa | last post by:
Hi, can someone please show me how to most elegently do this?..... I have a textbox, and I want to search the contents of it and replace all instances of a certain word, and replace that word...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
5
by: Neil | last post by:
"lyle" <lyle.fairfield@gmail.comwrote in message news:48c3dde7-07bd-48b8-91c3-e157b703f92b@f3g2000hsg.googlegroups.com... Question for you. I'm doing something similar, only, instead of opening...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.