By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 2,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

XSLT problem with single tags

P: n/a
Hi,

Got a litte problem here. I'm trying to create a XSLT file that will do
a transformation from WordML format (MS Word XML format, see
http://rep.oio.dk/Microsoft.com/offi...as/welcome.htm) to a
reasonably clean (X)HTML format.

(The reason being that, combined with some PHP scripting it should be
possible to store the embedded images, which is pretty neat).

I am, however running into a XSLT problem. An piece of an old version
works like this:

<xsl:template match="w:r">
<xsl:choose>
<xsl:when test=".//w:i">
<i><xsl:apply-templates /></i>
</xsl:when>
<xsl:when test=".//w:b">
<b><xsl:apply-templates /></b>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

This matches the r element (Run element, kind of a default container
thingy). It tests whether the r element contains an i or b element
(meaning of course that the content of that r element is in italic or
bold.) When this is the case, nice html style tags are placed. This
doesn't function properly in the case where an r element contains both
an i and a b element, i.e. when the text is both italic and bold.
Therefore, i changed the code to:

<xsl:template match="w:r">
<xsl:if test=".//w:i">
<i>
</xsl:if>

<xsl:if test=".//w:b">
<b>
</xsl:if>

<xsl:apply-templates />

<xsl:if test=".//w:i">
</i>
</xsl:if>

<xsl:if test=".//w:b">
</b>
</xsl:if>
</xsl:template>

It now tests twice for each style, for the opening tag and for the
closing tag. In principal this works fine, but in practice the xslt
sheet is not well-formed and will not be applied as it contains non
closed tags (the <i> and <b> tags). I've tried to:
- replace the < and > with &lt; and &gt;
- put the tages inside CDATA sections, for example <![CDATA[<i>]]>
However, in both cases the tags of appear as literal text instead of
HTML code.

Any ideas on how to able to insert single open or closing tags in my
HTML code, or another solution to properly nest the <i> and <b>
elements?

TIA

Wilco - Dwergkees - Menge

May 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


dwergkees wrote:

<xsl:template match="w:r">
<xsl:choose>
<xsl:when test=".//w:i">
<i><xsl:apply-templates /></i>
</xsl:when>
<xsl:when test=".//w:b">
<b><xsl:apply-templates /></b>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Why don't you simply do
<xsl:template match="w:r"><xsl:apply-templates /></xsl:template>

<xsl:template match="w:i">
<i><xsl:apply-templates /></i>
</xsl:template>

<xsl:template match="w:b">
<b><xsl:apply-templates /></b>
</xsl:template>

I am not familiar with WordML however, but based on what you have posted
and on how XSLT works it seems more natural to simply let
xsl:apply-templates do its work combined with templates for the
different elements you need to process.
--

Martin Honnen
http://JavaScript.FAQTs.com/
May 7 '06 #2

P: n/a
>Why don't you simply do
<xsl:template match="w:r"><xsl:apply-templates /></xsl:template>

<xsl:template match="w:i">
<i><xsl:apply-templates /></i>
</xsl:template>

<xsl:template match="w:b">
<b><xsl:apply-templates /></b>
</xsl:template>

I've tried fussin' about with your solution, but i can't get it to fit
just right. I'll show a small WordML example to demonstrate the problem
more clearly:

WordML:

<w:p>
<w:r>
<w:t>Plain text</w:t>
</w:r>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:t>Bold text</w:t>
</w:r>
<w:r>
<w:t> </w:t>
</w:r>
<w:r>
<w:rPr>
<w:i/>
</w:rPr>
<w:t>Italic text</w:t>
</w:r>
<w:r>
<w:rPr>
<w:b/>
<w:i/>
</w:rPr>
<w:t>Bold and italic</w:t>
</w:r>
</w:p>

Should transform to:

<p>Plain text <b>Bold text</b> <i>Italic text</i> <i><b>Bold and
italic</b></i></p>

As you can see, the <w:i/> and <w:b/> tags are grandchildren of the r
element, the text itself is a child of the r element. So at each r
element I want to check the existence of w:i and w:b and surround the t
element with the corresponding HTML. Your solution matches the
existence, but then the processor is at the wrong current Node. (As far
as I understand the complexities of xslt).

Any thoughts?

TIA

Wilco.

May 7 '06 #3

P: n/a
dwergkees wrote:
Why don't you simply do
<xsl:template match="w:r"><xsl:apply-templates /></xsl:template>

<xsl:template match="w:i">
<i><xsl:apply-templates /></i>
</xsl:template>

<xsl:template match="w:b">
<b><xsl:apply-templates /></b>
</xsl:template>

I've tried fussin' about with your solution, but i can't get it to fit
just right. I'll show a small WordML example to demonstrate the problem
more clearly:

WordML:

<w:p>
<w:r>
<w:t>Plain text</w:t>
</w:r>
<w:r>
<w:rPr>
<w:b/>
</w:rPr>
<w:t>Bold text</w:t>
</w:r>
<w:r>
<w:t> </w:t>
</w:r>
<w:r>
<w:rPr>
<w:i/>
</w:rPr>
<w:t>Italic text</w:t>
</w:r>
<w:r>
<w:rPr>
<w:b/>
<w:i/>
</w:rPr>
<w:t>Bold and italic</w:t>
</w:r>
</w:p>


This is an interesting relic of (a) the fact that Word uses out-of-line
markup and (b) the sedulous avoidance of Mixed Content common to those
who think pointers are more fun to program than trees. It's also about
the only way you can model the behaviour of unschooled authors in Word.

Just add another condition to your original:

<xsl:template match="w:r">
<xsl:choose>
<xsl:when test=".//w:i and .//w:b">
<i><b><xsl:apply-templates/></b></i>
</xsl:when>
<xsl:when test=".//w:i and not(.//w:b)">
<i><xsl:apply-templates/></i>
</xsl:when>
<xsl:when test=".//w:b and not(.//w:i)">
<b><xsl:apply-templates/></b>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Should transform to:

<p>Plain text <b>Bold text</b> <i>Italic text</i> <i><b>Bold and
italic</b></i></p>
No, there is no white-space after "Plain text" nor after "Italic text"
in your quoted XML document. If you need to introduce extra white-space
you need to specify the rules for doing so.
As you can see, the <w:i/> and <w:b/> tags are grandchildren of the r
element, the text itself is a child of the r element. So at each r
element I want to check the existence of w:i and w:b and surround the t
element with the corresponding HTML. Your solution matches the
existence, but then the processor is at the wrong current Node. (As far
as I understand the complexities of xslt).

Any thoughts?


Here's another way to do it, based on Martin's suggestion of using the
normal "apply-templates" way of proceeding down a document. You'll have
to jiggle the declared namespace for w: as I don't know what your
document declares it as. This method will handle anything occurring in
w:rPr, not just bold and italics.

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

<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="w:t"/>

<xsl:template match="w:p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>

<xsl:template match="w:r">
<xsl:choose>
<xsl:when test="w:rPr">
<xsl:call-template name="nest">
<xsl:with-param name="styles" select="w:rPr/*"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="nest">
<xsl:param name="styles"/>
<xsl:param name="counter">
<xsl:text>1</xsl:text>
</xsl:param>
<xsl:choose>
<xsl:when test="$counter>count($styles)">
<xsl:value-of select="w:t"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name($styles[$counter])}">
<xsl:call-template name="nest">
<xsl:with-param name="styles" select="$styles"/>
<xsl:with-param name="counter" select="$counter+1"/>
</xsl:call-template>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

In this, I am stripping all space except in w:t. This will preserve the
otherwise vulnerable white-space-only node.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
May 7 '06 #4

P: n/a
Thanks!

This is the kind of solution I was looking for!!! I have to tweak it
here and there, but this is just the kind of nesting principle I was
interested in, as it allows for extensions (I plan to use the same
nesting at higher level, so I can alternate between <p>, <h1>, <h2> to
<hn>. That should be possible right?)
Should transform to: <p>Plain text <b>Bold text</b> <i>Italic text</i> <i><b>Bold and
italic</b></i></p>


No, there is no white-space after "Plain text" nor after "Italic text"
in your quoted XML document. If you need to introduce


as for the extra whitespaces in the desired output, they are just
random typos from me! I'm just as happy with all the original
whitespaces minus unnecesary whitespace.
Again, thanks a lot for helping out both Martin and Peter!!

Wilco Menge

May 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.