473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

String parsing in XSL & XPath

AR
Hi,
I have an xml like this:

<a>
a 1
<b font-size="10">b 2</b>
x
<c font-size="14" font-weight="bold">c 3</c>
y
</a>

The output will be like below with the corresponding font size and
weight in HTML:

a 1 b 2 x c 3 y

How can I do it using XSL and XPath?

Thanks
AR

Jul 20 '05 #1
2 1553


AR wrote:

I have an xml like this:

<a>
a 1
<b font-size="10">b 2</b>
x
<c font-size="14" font-weight="bold">c 3</c>
y
</a>

The output will be like below with the corresponding font size and
weight in HTML:

a 1 b 2 x c 3 y
That does not look like HTML at all to me but perhaps the following
How can I do it using XSL and XPath?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html" />

<xsl:template match="b | c">
<span>
<xsl:choose>
<xsl:when test="@font-size and @font-weight">
<xsl:attribute name="style">
<xsl:text>font-size : </xsl:text>
<xsl:value-of select="@font-size" />
<xsl:text>px; font-weight : </xsl:text>
<xsl:value-of select="@font-weight" />
</xsl:attribute>
</xsl:when>
<xsl:when test="@font-size">
<xsl:attribute name="style">
<xsl:text>font-size : </xsl:text>
<xsl:value-of select="@font-size" />
<xsl:text>px</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:when test="@font-weight">
<xsl:attribute name="style">
<xsl:text>font-weight : </xsl:text>
<xsl:value-of select="@font-weight" />
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:apply-templates />
</span>
</xsl:template>

</xsl:stylesheet>

is what you are looking for, result of the transformation is

a 1
<span style="font-size : 10px">b 2</span>
x
<span style="font-size : 14px; font-weight : bold">c 3</span>
y
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
AR
Hi,
Your are right. I want the html output what you have done.
Let me explain more what I needed. I have an xml like below ....
<html>
<a>
a 1
<b font-size="10">b 2</b>
x
<c font-size="14" font-weight="bold">c 3</c>
y
</a>

<a>
pqr
<w font-size="8">www1</w>
abc
</a>

<a>
testing 1
<r font-size="16" font-weight="bold">ALPHA</r>
done
</a>
</html>

There will be multiple <a> elements in the same level and each <a>
element will have different child nodes and text. The main thing I want
is to produce HTML from the xml. What more xsl is needed to support
multiple <a> elements? Thank you very much.

Martin Honnen wrote:
AR wrote:

I have an xml like this:

<a>
a 1
<b font-size="10">b 2</b>
x
<c font-size="14" font-weight="bold">c 3</c>
y
</a>

The output will be like below with the corresponding font size and
weight in HTML:

a 1 b 2 x c 3 y


That does not look like HTML at all to me but perhaps the following
How can I do it using XSL and XPath?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html" />

<xsl:template match="b | c">
<span>
<xsl:choose>
<xsl:when test="@font-size and @font-weight">
<xsl:attribute name="style">
<xsl:text>font-size : </xsl:text>
<xsl:value-of select="@font-size" />
<xsl:text>px; font-weight : </xsl:text>
<xsl:value-of select="@font-weight" />
</xsl:attribute>
</xsl:when>
<xsl:when test="@font-size">
<xsl:attribute name="style">
<xsl:text>font-size : </xsl:text>
<xsl:value-of select="@font-size" />
<xsl:text>px</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:when test="@font-weight">
<xsl:attribute name="style">
<xsl:text>font-weight : </xsl:text>
<xsl:value-of select="@font-weight" />
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:apply-templates />
</span>
</xsl:template>

</xsl:stylesheet>

is what you are looking for, result of the transformation is

a 1
<span style="font-size : 10px">b 2</span>
x
<span style="font-size : 14px; font-weight : bold">c 3</span>
y
--

Martin Honnen
http://JavaScript.FAQTs.com/


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
8
by: Joe Pannone | last post by:
Does anyone have an example on using an XPath statement (in VB.NET) where the source XML document is NOT a file, but a 'text' value from SQL Server?
10
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to...
2
by: alex masselot | last post by:
Hello I'm not familiar with xerces in c++ Currently, we parse xml file with perl (typically XML::Twig) and java (dom4j). With both API, there is a very comfortable way to mix Sax/DOM, by...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
0
by: bruce | last post by:
Hi. a pretty simple question, i'm guessing. i have a text/html string that looks like: ....(A&E) the issue i have is that when i parse it using xpath/node/toString, i get the following
0
by: Fredrik Lundh | last post by:
bruce wrote: that's because your parser is interpreting the &E part as an entity reference, and the serializer is then adding the missing semicolon. bare ampersands must be written as "&amp;" in...
0
by: bruce | last post by:
Hi Fredrick Thanks for the reply. But since I don't have control of the initial text, is there something with python that will strip/replace this... or are you saying I should do a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.