xslt help needed with element nodes embedded in text node

V

Volker Lenhardt

I'm but an amateur with xsl stylesheets. I need some help to the output
of text nodes with embedded element nodes like the following:

<uf><ns>This</ns> example</uf>
<uf>Another <ns>This</ns> example</uf>
<uf>Another <ns>This</ns></uf>

I need to output the ns strings in italics.

<xsl:apply-templates select="./uf" />

<xsl:template match="ns">
<i><xsl:value-of select="." /></i>
</xsl:template>

As long as the xml file is exported in lines as above the output is ok:
"This example". (In this email I don't indicate the italicised part string.)

But lately the document server hosting the application that contains xml
files of the sort above changed the export of xml files. Now there are
line breaks at each new element boundary:

<uf>
<ns>This</>
example
</uf>

With my xsl code I get an additional space at the beginning and the end
of the phrase: " This example ". I tried to get rid of them with

<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>

But this results in "Thisexample".

What is to be done? (There is no option to substitute the mandatory
spaces with  ).

Volker
 
A

Alain Ketterlin

Volker Lenhardt said:
I'm but an amateur with xsl stylesheets. I need some help to the
output of text nodes with embedded element nodes like the following:

Sorry to be pedantic, but there is no such thing as a "text node with
embedded element nodes". In your example:
<uf><ns>This</ns> example</uf>

You have an element (uf) containing two nodes: an element (ns) and a
text node.
<xsl:apply-templates select="./uf" />

<xsl:template match="ns">
<i><xsl:value-of select="." /></i>
</xsl:template>

Looks ok. Except that I would not advise to use default templates (on uf).
<uf>
<ns>This</>
example
</uf>

The uf element contains a text node, an element, and a second text node.
With my xsl code I get an additional space at the beginning and the
end of the phrase: " This example ".

It should be "[EOL][SPC][SPC]<i>This</i>[EOL][SPC][SPC]". These spaces
are in the original document, and there is no way to guess that they are
not significant, especially since element uf has mixed content.
I tried to get rid of them with

<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>

But this results in "Thisexample".

That's expected, your template applies to all text nodes.

What you want is to remove spaces 1) at the beginning of the first child
of uf if it is a text node, and 2) at the end of the last child of uf if
it is a text node. Something like (untested):

<xsl:template match="uf">
<xsl:apply-templates select="*|text()">
<xsl:with-param name="p" select="position()"/>
</xsl:apply-templates>
</xsl:template>

and, in the template on text(), check whether you need to remove
something.

But really, you should fix the thing producing the document.

-- Alain.
 
V

Volker Lenhardt

Am 23.02.2012 08:38, schrieb Alain Ketterlin:
Sorry to be pedantic, but there is no such thing as a "text node with
embedded element nodes". In your example:


You have an element (uf) containing two nodes: an element (ns) and a
text node.

OK. I understand.
What you want is to remove spaces 1) at the beginning of the first child
of uf if it is a text node, and 2) at the end of the last child of uf if
it is a text node. Something like (untested):

<xsl:template match="uf">
<xsl:apply-templates select="*|text()">
<xsl:with-param name="p" select="position()"/>
</xsl:apply-templates>
</xsl:template>

Thank you. This helped me to get the problem under control. I can now
recognize the text at the first and the last position (and of course the
ns elements). But at the moment I don't see how to only lefttrim the
first and to only righttrim the last. Is there a function around?
But really, you should fix the thing producing the document.

No chance. It's part of the xml schema recommended by the German
National Library to export a bibliographical data record from a MAB
catalog database.

Volker
 
V

Volker Lenhardt

Hi Alain,

Am 23.02.2012 14:16, schrieb Volker Lenhardt:
But at the moment I don't see how to only lefttrim the
first and to only righttrim the last. Is there a function around?

I've found some template code on the net.

Volker
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top