By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Replace all instances of an element

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a


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

P: n/a
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...

<snip>

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

P: n/a

"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

P: n/a


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

P: n/a

"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

P: n/a
<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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.