String parsing in XSL & XPath

A

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
 
M

Martin Honnen

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:eek:utput 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
 
A

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.
 

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,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top