Re: [xsl] Saxon 9.4 <bold></bold> Transformed to (newline)</bold> Problem

Subject: Re: [xsl] Saxon 9.4 <bold></bold> Transformed to (newline)</bold> Problem
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Tue, 15 Jan 2013 11:10:59 -0500
Raymond,

I don't know if switching xsl:output/@indent to 'no' will help; or at
least, I don't believe that Saxon will will introduce a newline in
mixed content even with @indent='yes', and I can't duplicate the
behavior. So your problem is puzzling. Please tell us if @indent='no'
fixes it.

If not you may have to tell us more. For example, we need to make sure
you aren't opening the file in a tool (XML Spy?) that is "helpfully"
introducing the new line. Try a plain text editor to confirm that it
is really there in the result that Saxon is producing. Also tell us
how you are running Saxon; if you aren't using its serializer, then
whatever environment you are in may be the culprit.

You also pose a couple of follow-on questions. Yes, you can remove the
empty bold element, like so:

<xsl:template match="bold[not(exists(*) or normalize-space(.))]"/>

This has the effect of matching any 'bold' element that has no
elements inside and no text value other than whitespace, and dropping
it. (Note you will also lose any comments or processing instructions
that happen to be in there, but you will ordinarily lose them anyway.)

However, this won't work with your current stylesheet because you are
not actually processing the 'bold' element. You have this template:

<xsl:template match="level1|level2|level3|level4|level5|level6">
        <xsl:copy-of select="./node()" copy-namespaces="no" />
</xsl:template>

which is matching the 'level1' element in your example and copying its
contents. Accordingly none of its children or descendants are matched
with templates.

Change the copy-of instruction to an xsl:apply-templates and you will
be able to match and process the 'bold' element (unless you have other
code that interferes with the normal behavior).

I hope this helps.

Cheers,
Wendell

On Tue, Jan 15, 2013 at 9:14 AM, Raymond Lillibridge
<RLillibridge@xxxxxxxxxxxx> wrote:
> List members,
>
> Due to some batch processing, some of my input XML may have empty elements.
>
> Here is some sample XML:
> <level1>
> <para> Here is some text inside a para tag. <bold></bold> Note that the
'bold' element before the word Note is empty.  I would like it to stay that
way without the insertion of a newline.</para> </level1>
>
>
> When I transform this XML, using Saxon 9.4, the <bold></bold> element is
getting converted similar to the following:
> <level1>
> <para> Here is some text inside a para tag.
>          <bold/> Note that the 'bold' element before the word Note is empty.
I would like it to stay that way.</para> </level1>
>
>
> The Problem:
> Due to further batch processing needs, I do not want the insertion of a
newline before the <bold/> element, which is being created after running an
XSLT transformation on the sample XML above.  (XMLSpy does not insert the
newline, by the way, but I want to use Saxon for my transformation.)  In my
xsl file I do not have an explicit template match for the 'bold' element.
>
> My XSL:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:fo="http://www.w3.org/1999/XSL/Format";
> xmlns:mcc="http://www.municode.com/xslt";>
>
> <xsl:strip-space elements="*" />
> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
>
> <!-- To Get:  {$InputDocPath} -->
> <xsl:include href="./MCC_LIB.xsl"/>
>
> <xsl:template match="/">
>         <xsl:result-document href="{$InputDocPath}/Book_ALL.xml">
>                 <xsl:apply-templates select="node()" />
>         </xsl:result-document>
> </xsl:template>
>
> <xsl:template match="book">
>                 <xsl:element name="book">
>                         <xsl:element name="bookinfo">
>                                 <xsl:element name="title"></xsl:element>
>                                 <xsl:element name="subtitle"></xsl:element>
>                         </xsl:element>
>                         <xsl:apply-templates select="node()"/>
>                 </xsl:element>
> </xsl:template>
>
>
> <xsl:template match="level1|level2|level3|level4|level5|level6">
>         <xsl:copy-of select="./node()" copy-namespaces="no" />
</xsl:template>
>
>
> <!-- CATCH-ALL ==================================================== -->
>         <xsl:template match="@*|node()">
>                 <xsl:copy-of select="./node()" copy-namespaces="no" />
>         </xsl:template>
> </xsl:stylesheet>
>
>
>
> Looking in the Saxon documentation, I was not able to find a switch to
control the transformation behavior that changes the <bold></bold> to
(newline)<bold/>.
>
> I'd rather not use XMLSpy since all my other batch transformations are using
Saxon.
> If there is a configuration switch for Saxon, could someone direct me where
I may learn about it.
>
> Or, would it be more practical to write a template to remove the "empty"
<bold></bold> element, or better yet, remove all empty elements?  I don't know
how this would be written, and would appreciate any insights someone may
offer.
>
>
> Kind regards,
>
> Raymond Lillibridge
> Sr. Software Engineer
> rlillibridge@xxxxxxxxxxxx
> Municipal Code Corporation | Facebook | Twitter
>



--
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^

Current Thread