Newbie XML Stylesheet help

T

tony vee

Hi

I am trying to write an XML stylesheet and I need a bit of help. I have an
XML document which contains,


<img src="currentLocation/myPicture.gif">


I want to change to change the attributes of all these elements to


<img src="BlackboardLocation/myPicture.gif">


To do this, I have a Java class which takes in an XML document and an XSL
stylesheet, but I don't know how to write the XSLT to do what I want. First,
can I write something like this to find each img element?





What I want to know is whether I can use XSL to pick out individual
elements, change their values and then write back the new values without
disturbing the rest of the document? Or do I need to get all of the
elements, change the attribute values of the img element, and then write all
of the elements back to the document?


Hopefully this is clear, but probably it is not!

Tony
 
M

Marrow

Hi Tony,

It would appear that you just need an identity copy stylesheet with a
special template for handling the img/@src attribute - something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml"/>
<!-- templates for main identity copying -->
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

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

<xsl:template match="@* | comment() | text() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<!-- template for special node handling -->
<!-- e.g. special handling for img/@src -->
<xsl:template match="img/@src">
<xsl:attribute name="src">
<xsl:text>BlackboardLocation/</xsl:text>
<xsl:value-of select="substring-after(.,'/')"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 
J

Jim

Hi,

If what you need to do is go a 'global find and replace' in a file, try this
perl command:

perl -ape "s/(<img src=\").*(\/.*\.gif\">)/\1BlackboardLocation\2/"
yourFile.xsl

This will find all the 'img' tags and replace the first path element with
'Blackboard'.

Hope this helps.

jim cant

p.s. If you prefer a perl script, try

# Reads standard input writes to stdandard output
# changing <currentLocation> to 'BlackboardLocation' in
# patterns begining with
#
# <img src="
#
# and ending with
#
# /myPicture.gif">
#
# where 'myPicture' can be any string
# and <currentLocation> is the string following 'src="'
# up to the first '/'.

# Feed this script to itself and you should see the expected
# changes in the lines following __DATA__.

$newLocation = "BlackboardLocation";

while (<>)
{
s/(<img src=").*(\/.*\.gif">)/\1$newLocation\2/;
print;
}

__DATA__
first <img src="currentLocation/myPicture.gif"> first
second <img src="CHANGEHERE_1/myPicture.gif">second
third <img src="currentLocation/NO_CHANGEHERE_2.gif">third
forth <img src="CHANGEHERE_3/NO_CHANGEHERE_4.gif"> forth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top