Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 25th, 2007, 09:43 AM
Registered User
 
Join Date: Dec 2007
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
 
Old December 26th, 2007, 05:36 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old December 26th, 2007, 07:08 AM
Registered User
 
Join Date: Dec 2007
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

 
Old December 26th, 2007, 07:23 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.