f



xslt help needed with element nodes embedded in text node

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 &#160;).

Volker
0
2/22/2012 5:56:38 PM
comp.text.xml 8781 articles. 0 followers. Post Follow

4 Replies
798 Views

Similar Articles

[PageSpeed] 12

I tried <xsl:strip-space elements="uf" />.

It doesn't help either.

Volker
0
2/23/2012 7:05:48 AM
Volker Lenhardt <volker.lenhardt@uni-duisburg-essen.de> writes:

> 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.
0
alain14 (278)
2/23/2012 7:38:49 AM
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:
>
>> <uf><ns>This</ns>  example</uf>
>
> 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
0
2/23/2012 1:16:32 PM
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
0
2/23/2012 1:34:02 PM
Reply: