473,657 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT problem with single tags

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:otherwis e>
<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
4 1796


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:otherwis e>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Why don't you simply do
<xsl:template match="w:r"><xs l: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
>Why don't you simply do
<xsl:template match="w:r"><xs l: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
dwergkees wrote:
Why don't you simply do
<xsl:template match="w:r"><xs l: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:appl y-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:otherwis e>
<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:styleshe et 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:otherwis e>
<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:otherwis e>
<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="$counte r+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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2741
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a straight XML file http://www.discovertravelandtours.com/test/templates/test.xml?Location=Germany To another XML file http://www.discovertravelandtours.com/test/templates/test2.xml?Location=Germany
6
2909
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
9
1864
by: Jeff Rubard | last post by:
I am curious to know whether anyone has experience using XSLT for web XML (non-XHTML) styling, either with CSS or standalone. I myself have engaged in rather unsuccessful experiments with the DocBook chunk stylesheet, but it seems to me that retaining the structural markup of XML formats (i.e., the marking out of salient properties of a text in "anticipation" of RDF "semantics") demands something more powerful than CSS as it is known to...
1
5577
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records. I have specified <xsl:output method="html"/> and encoding="iso-8859-1". When I apply the xsl:value-of and set the disable-output-escaping to "yes", the HTML formatting tags are displayed correctly, but the French accented characters are...
2
5065
by: Joe | last post by:
How would you preserve tags from an XML element in XSLT output.. Example: XML document has an ELEMENT "SECTION" allowing contained HTML This element might have an element such as <A HREF="dfksdfaj">go</A> I want the output from XSLT processing of the SECTION elements to output
4
3161
by: Luke Dalessandro | last post by:
I have some XML data that has mixed content XML tags that embed XHTML tags, for instance: <note>Somebody wrote this note in XHTML and wanto to <a href="link.html" target="_new">link</a> to a particular tag, and was also pretty sure that they wanted the following <ul><li>two</li><li>items</li></ul> to appear as a list. To make matters worse <sarcastic>how could this be worse?</sarcastic>, the XHTML can be mixed with our own tags.</note>
3
1767
by: Paul Hatcher | last post by:
Hi Is it possible to control the node style that XslTransform uses to output XML? I'm trying to convert a file from XML to RDF and if I do the transform in XmlSpy I get the following <mySchema rdf:resource='fred' />
9
1777
by: daph4ntom | last post by:
Right, I need some some views on the pros and cons of using XSLT versus ASP.NET 2.0 for the Presentation layer of an app. My company are looking at creating multiple sites and multi lingual support going forward... I have looked at Chris Lovetts post http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml02192001.asp Some of his views are still relevant.
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.