Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 25th, 2007, 09:43 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Access/change html-tags contained in elementvalues

Hi,

I'm using php XMLWriter to create XML dynamically for transformation with an XSL-File. The data is obtained from a database.
Some XML element values contain HTML with e.g. img-tags in it:
Code:
<page>
  <text>
    Here are some images: &lt;img src=&quot;pic1.jpg&quot; &gt; und &lt;img src=&quot;pic.jpg&quot;&gt;
  </text>
</page>
As you can see the contents are automatically escaped (using e.g. php->XMLWriter->createElement()).
Now I would like to change the path of the images set in the src-attribute of the img-tags to e.g. images/pic1.jpg and imgaes/pic2.jpg. I want to avoid storing the location of the images within the HTML-contents.

Here's my approach that doesn't work:
Code:
<xsl:param name="urlImages" select="'images/'"/>

<xsl:template match="page/text">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="img/@src">
  <xsl:attribute name="src">
    <xsl:value-of select="concat($urlImages,.)"/>
  </xsl:attribute>  
</xsl:template>
Question:
Is there a way to access the src-attribute of the img-tags?

Thanks and best regards
polarbear
Reply With Quote
  #2 (permalink)  
Old December 26th, 2007, 05:36 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Because you are adding the img elements as text rather than XML what you want to do is extremely difficult. It would involve parsing the strings, unless you are using XSLT 2.0 then this is a really painful process. The easiest way to solve this consists of two steps. Firstly make sure the img data is well formed XML, instead of HTML use XHTML so the img elements are closed:
Code:
<img src="..." />
You don't even have to use XHTML as you will be transforming anyway, as long as the data is well formed, for example you could use:
Code:
<image source="..." />
In the transform stage you turn this into normal (X)HTML.

Then add them as elements, not strings. I am unfamiliar with PHP's XML library but this will mean modifying how the database data is turned into XML>

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old December 26th, 2007, 07:08 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Joe,

thanks for replying.

Code:
Then add them as elements, not strings. I am unfamiliar with PHP's XML library but this will mean modifying how the database data is turned into XML
.
Just to understand you right, is this what you mean:
Code:
<page>
  <text1>Here are some images: </text1>
  <img src="pic1.jpg" />
  <text2> and </text2>
  <img src="pic2.jpg" />
  <text3> Some text following.. </text3>
</page>
polarbear

Reply With Quote
  #4 (permalink)  
Old December 26th, 2007, 07:23 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Yes. Then you'll be able to manipulate the XML easily.

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change your own tags in the forum? KingCool BOOK: ASP.NET Website Programming Problem-Design-Solution 3 April 17th, 2007 01:08 AM
Render HTML tags jacksprophet XSLT 1 December 28th, 2006 07:03 PM
remove html tags lucian Dreamweaver (all versions) 1 November 14th, 2004 03:25 PM
Access to data contained on the clipboard. voneimin BOOK: Beginning Java 2 0 November 18th, 2003 12:25 AM



All times are GMT -4. The time now is 02:08 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.