Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I translate a HTML tag in a text...

Status
Not open for further replies.

Yaa101

Programmer
May 2, 2002
22
0
0
NL
How can I translate a HTML tag in a text...

I explane

I have a source:

Code:
<preface>
		  <par>Mijn naam is Bas Burger en ik zoek werk als PHP - XML programeur en/of webmaster.<br />
			Ik heb 17+ jaar ervaring met computers en sinds 1987 bezit ik een pc.<br />
			Het probleem is dat ik het verkeerde beroep had toen er nog een internet hype 
			bestond waar met zeer dure opleidingen gesmeten werd om de beroepstak af te sluiten voor nieuwkomers.<br />
			Dit is de meest belangrijke reden dat ik dure opleidingen en certificaten ontbeer.<br />
			Ik spreek en schrijf de engelse taal zeer goed en ben wat programeren betreft zeer gericht
			op de angel-saksische invloedsfeer.</par>
			<par>Ik heb alles zien komen wat vandaag de dag als heel normaal wordt ervaren zoals windows,
			internet etc.<br />
			En ben daardoor zeer praktijk gericht (gewoon doen en proberen zonder bang te zijn of het wel lukt)
			en heb ook een zeer brede ervaring.</par>
		</preface>

In there you see
Code:
<br />
tags that i want to have translated 1 to 1 into the new output...

Following template match
Code:
<xsl:template match=&quot;preface&quot;>
	  
		<xsl:element name=&quot;div&quot;>
		  
			<xsl:attribute name=&quot;class&quot;>preface</xsl:attribute>
		  <xsl:for-each select=&quot;par&quot;>
				  
		  		<xsl:element name=&quot;p&quot;>

		  				<xsl:value-of select=&quot;.&quot;/>
		
		      </xsl:element>
		    </xsl:for-each>
		 
		</xsl:element>
		
	</xsl:template>

Gives this output:
Code:
<div class=&quot;preface&quot;>
<p>Mijn naam is Bas Burger en ik zoek werk als PHP - XML programeur en/of webmaster.
			Ik heb 17+ jaar ervaring met computers en sinds 1987 bezit ik een pc.
			Het probleem is dat ik het verkeerde beroep had toen er nog een internet hype 
			bestond waar met zeer dure opleidingen gesmeten werd om de beroepstak af te sluiten voor nieuwkomers.
			Dit is de meest belangrijke reden dat ik dure opleidingen en certificaten ontbeer.
			Ik spreek en schrijf de engelse taal zeer goed en ben wat programeren betreft zeer gericht
			op de angel-saksische invloedsfeer.</p>
<p>Ik heb alles zien komen wat vandaag de dag als heel normaal wordt ervaren zoals windows,
			internet etc.
			En ben daardoor zeer praktijk gericht (gewoon doen en proberen zonder bang te zijn of het wel lukt)
			en heb ook een zeer brede ervaring.</p>
</div>

I need this output:
Code:
<div class=&quot;preface&quot;>
<p>Mijn naam is Bas Burger en ik zoek werk als PHP - XML programeur en/of webmaster.<br />
			Ik heb 17+ jaar ervaring met computers en sinds 1987 bezit ik een pc.<br />
			Het probleem is dat ik het verkeerde beroep had toen er nog een internet hype 
			bestond waar met zeer dure opleidingen gesmeten werd om de beroepstak af te sluiten voor nieuwkomers.<br />
			Dit is de meest belangrijke reden dat ik dure opleidingen en certificaten ontbeer.<br />
			Ik spreek en schrijf de engelse taal zeer goed en ben wat programeren betreft zeer gericht
			op de angel-saksische invloedsfeer.</p>
<p>Ik heb alles zien komen wat vandaag de dag als heel normaal wordt ervaren zoals windows,
			internet etc.<br />
			En ben daardoor zeer praktijk gericht (gewoon doen en proberen zonder bang te zijn of het wel lukt)
			en heb ook een zeer brede ervaring.</p>
</div>

I need output with the breaks left in.

Thanks on front.
 
At the moment the <br /> is inerpreted as a child node of par with no value.

you could leave out the <br /> alltogether and make your xml look like this:
<par>Mijn naam is Bas Burger en ik zoek werk als PHP - XML programeur en/of webmaster.</par>

the xsl wil look like this:

<xsl:for-each select=&quot;par&quot;>

<xsl:element name=&quot;p&quot;>

<xsl:value-of select=&quot;.&quot;/><br />

</xsl:element>
</xsl:for-each>

 
I got and found the answer yesterday in here:
Check it out.

I thought the same as you at start but decided that the situation i put down is a everyday problem, you should be able to put data like that with tags left in...
That is why i decided to ask and part of my answer was given there and part i found myself.

Bassie.
 
Thanks, I wanted to show you the '&gt' sollution but when I tested it it would just give me a <br /> as result (no new line).
The disable-output-escaping=&quot;yes&quot; made it work.
 
Yup i found that one myself as well...

This way you can use this to output parts of text or html without a need to parse all seperate parts...

Both parsing and unparsing data can be a good thing, depending on the things you need to do.
I did not need to parse these parts each seperate i only wanted to keep the paragraph rendering intact...


leuk he? dat er meerdere problemen worden opgelost met 1 probleem, daarom vraag ik dit soort dingen en public...

Bas.
 
you can also use the <CDATA > tag which doesn't look at syntax until it finds </ CDATA>.

(PS als je nog steeds werk zoekt als XML programmeur:
 
Thanks Ploegah...

As you can see the problem was a part of my cv...
I had no plan to spam on here.

Ja ik zoek nog steeds werk...
Bedankt voor de link... :)

Can you explane in what context you use the forenamed CDATA tag?
Also I found it's not that easy to put in external links and external languages like javascript and php.
The php bit i solved with preg-replace (expensive) but still looking in other ways...

Bas.
 
hi Yaa10,

im trying to view a xml file with xsl and i think i have the opposite problem than you had, i have a large text between xml tags wich looks like this:


<mTitle>hello all you crazy people
how are you doing
</mTitle>

now when i present it i want to keep the line break on the view, but instead of line break i get a blank.

any idea?

thanx.
 
for raikyng:

This is the xml file:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;test.xsl&quot;?>
<catalog pref=&quot;hello&quot;>
<book id=&quot;bk102&quot;>
<genre>Fantasy
Second line


This seems to work ok
</genre>
</book>
</catalog>


This is the xsl:

<?xml version='1.0'?>
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;catalog&quot; mode=&quot;grp_categ&quot;/>
</xsl:template>


<xsl:template match=&quot;catalog&quot; mode=&quot;grp_categ&quot;>
<xsl:param name=&quot;replace&quot;>&#10;</xsl:param>
<xsl:param name=&quot;with&quot;>&lt;br&gt;</xsl:param>
<xsl:for-each select=&quot;book&quot;>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;genre&quot;/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name=&quot;replace&quot;>
<xsl:param name=&quot;string&quot;/>
<xsl:choose>
<xsl:when test=&quot;contains($string,'&#10;')&quot;>
<xsl:value-of select=&quot;substring-before($string,'&#10;')&quot;/>
<br/>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;substring-after($string,'&#10;')&quot;/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select=&quot;$string&quot;/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
 
hi harmmeijer, thanx for the fast reply!!!

i've tried your code but nothing happens....


i think it's because of my IE5.5, anyway this is my code together with your code, maybe i forgot something....

the xml:

<?xml version=&quot;1.0&quot; encoding=&quot;windows-1255&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;template.xsl&quot;?>
<news maxRec=&quot;3&quot;>

<message id=&quot;1&quot;>
<mDate>01/05/2002</mDate>
<mTitle>
hello
you
</mTitle>
<mLink>fr_Odot.htm</mLink>
</message>

<message id=&quot;2&quot;>
<mDate>01/05/2002</mDate>
<mTitle>
whats up?
y'all
</mTitle>
<mLink>fr_Odot.htm</mLink>
</message>

<message id=&quot;3&quot;>
<mDate>01/05/2002</mDate>
<mTitle>
nothing much
i guess
</mTitle>
<mLink>fr_Odot.htm</mLink>
</message>

</news>

the xsl:

<?xml version=&quot;1.0&quot; encoding=&quot;windows-1255&quot;?>

<xsl:stylesheet xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;news&quot; mode=&quot;grp_news&quot;/>
</xsl:template>

<xsl:template match=&quot;news&quot; mode=&quot;grp_news&quot;>
<H2>messages</H2>
<xsl:param name=&quot;replace&quot;> </xsl:param>
<xsl:param name=&quot;with&quot;><br></xsl:param>

<xsl:for-each select=&quot;message&quot;>
<SPAN STYLE=&quot;font-style:italic&quot;>Title: </SPAN>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;mTitle&quot;/>
</xsl:call-template>
<SPAN STYLE=&quot;font-style:italic&quot;>Link: </SPAN>
<xsl:with-param name=&quot;string&quot; select=&quot;mLink&quot;/><BR />
</xsl:for-each>
</xsl:template>

<xsl:template name=&quot;replace&quot;>
<xsl:param name=&quot;string&quot;/>
<xsl:choose>
<xsl:when test=&quot;contains($string,' ')&quot;>
<xsl:value-of select=&quot;substring-before($string,' ')&quot;/>
<br/>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;substring-after($string,' ')&quot;/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select=&quot;$string&quot;/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

thanx.
 
Start with taking out your second line of your xml datafile to start with...

That file is no XSL... :)

checking other things...
 
You are using a different namespace:
Your namespace:
My namespace:

The xsl code is messed up because the
was interpreted as a linefeed.

Try this xsl:
<?xml version='1.0'?>
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<xsl:apply-templates select=&quot;news&quot; mode=&quot;grp_news&quot;/>
</xsl:template>

<xsl:template match=&quot;news&quot; mode=&quot;grp_news&quot;>
<xsl:param name=&quot;replace&quot;>
</xsl:param>
<xsl:param name=&quot;with&quot;><br/></xsl:param>
<H2>messages</H2>

<xsl:for-each select=&quot;message&quot;>
<SPAN STYLE=&quot;font-style:italic&quot;>Title: </SPAN>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;mTitle&quot;/>
</xsl:call-template>
<SPAN STYLE=&quot;font-style:italic&quot;>Link: </SPAN>
</xsl:for-each>
</xsl:template>

<xsl:template name=&quot;replace&quot;>
<xsl:param name=&quot;string&quot;/>
<xsl:choose>
<xsl:when test=&quot;contains($string,'
')&quot;>
<xsl:value-of select=&quot;substring-before($string,'
')&quot;/>
<br/>
<xsl:call-template name=&quot;replace&quot;>
<xsl:with-param name=&quot;string&quot; select=&quot;substring-after($string,'
')&quot;/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select=&quot;$string&quot;/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>




----You allso need MSXML3 if youre using ms internet explorer. (windir\system32\msxml3.dll). If you do not have it check ms site you can download it as an active x control (CAB file)
 
Well it is sht again.
The character that won't display is:
& # 1 0 ;
But than without the spaces.
 
hi,

thanx alot for your help, but steel no success.

i guess this works only at IE6.0, i can only see the html output.

my solution was difrent:

the xsl:

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:template match=&quot;/&quot;>

<xsl:for-each select=&quot;news/message&quot;>

<div class=&quot;ListScrool&quot; dir=&quot;rtl&quot;>
<span class=&quot;NewsDate&quot;><xsl:value-of select=&quot;mDate&quot;/></span>
<br/>
<xsl:element name=&quot;a&quot;>
<xsl:attribute name=&quot;href&quot;>
javascript:sendParams(&quot;<xsl:value-of select=&quot;mLink&quot;/>&quot; , &quot;<xsl:value-of select=&quot;mDate&quot;/>&quot; , &quot;&quot;<xsl:for-each select=&quot;mTitle/br&quot;>+&quot;<xsl:value-of select=&quot;.&quot;/>\n&quot;</xsl:for-each>)
</xsl:attribute>

<xsl:attribute
name=&quot;class='News'&quot;></xsl:attribute>
<xsl:for-each select=&quot;mTitle/br&quot;>
<xsl:value-of select=&quot;.&quot;/><br/>
</xsl:for-each>
</xsl:element>
</div>
<br/>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>


the xml:
<?xml version=&quot;1.0&quot; encoding=&quot;windows-1255&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;template.xsl&quot;?>

<news MaxRec=&quot;3&quot;>
<message>
<mID>1</mID>
<mDate>01/05/2002</mDate>
<mTitle><br>blablabla</br></mTitle>
<mLink>C:/data.exp</mLink>
</message>
<message>
<mID>2</mID>
<mDate>01/05/2002</mDate>
<mTitle><br>blablabla</br><br>blablabla</br></mTitle>
<mLink>C:/AUTOEXEC.CRY</mLink>
</message>
<message>
<mID>3</mID>
<mDate>01/05/2002</mDate>
<mTitle><br>blablabla</br><br>blablabla</br></mTitle>
<mLink>/OdotMisrad/Library/</mLink>
</message>
<message>
<mID>3</mID>
<mDate>01/05/2002</mDate>
<mTitle>
<br>blablabla</br>
<br>blablabla</br>
<br>blablabla</br>
</mTitle>
<mLink>/OdotMisrad/Library/</mLink>
</message>
</news>


im using this namespace because your namespace doesnt work for me i guess again because my IE.

thanx alot, raikyng.
 
Sorry after watching this i still prefer my own thing with the CDATA and the br tags in the text...

Please tell me the pro's of this one over mine?

 
The text could have been/will be filled in by somebody who does not care to put a <br/> for every linefeed.
The information is provided as is but you have to display it properly in html, the only way to do this is replace the lf with <br/>.
Or replace the crlf with <br/> when you insert the data in a database (I am sure it comes from there anyway).

Again for rayking, you are using a diffrent namespace:
should be:

I suggest you copy and paste the entire xsl text, it should work.
If not you need the dll
----You allso need MSXML3 if youre using ms internet explorer. (windir\system32\msxml3.dll). If you do not have it check ms site you can download it as an active x control (CAB file)
IE 5.5 does not have this dll. (don't know about IE6)
I see in the url that Yaa101 put in that it is version 4 allready, here is the download url again:
 
True, but since i control the xml files i can also generate the br tags without people to notice when inserting text...

I mean if i offer some user a input box i know that i have to translate it myself...

Often these are relative short texts in which certain characters can be replaced with regular expressions (expensive speed wise i know)...

Don't get me wrong i ask this all as i am learning XSLT myself and sometimes say things to get a reaction so i can compare my own thoughts with other peoples...

I still find that this recursive solution has a few mishaps as well and find it also a expensive solution speed wise to use a recursive template...

I know my solution is not a conforming method... but works smooth...

My thinkworld comes from PHP so my solutions are also based on what i have learned in that language...
 
Yaa101, you are correct.

In my opinion I would only use this sollution if the data is updated a lot more times than that it is viewed.
Or when I do not have control over it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top