Altova Mailing List Archives


Re: [xsl] Dealing with breaking out mixed content

From: Chris Loschen <loschen@------------->
To:
Date: 12/9/2002 12:21:00 PM
Yes, that was it! Thank you very much! (It's always the small things, isn't 
it...)



I'll let you know when I've got the final touches worked out, but I think 
I've got it in a state where it's working. Many thanks!



At 04:40 PM 12/9/02, you wrote:
Hi Chris,



Chris Loschen wrote:
Thank you very much, Jeorg.

I think I'm a lot closer now, but something I'm doing still isn't quite 
working. I had to modify your code slightly (I hope) to handle some of 
the cases outside the sample I gave, but I think I kept the basic 
functionality. However, what I'm getting is the first set of sub-nodes, 
but none of the rest. So, for my example, I got

only:

<p class="extract-9"><span class="extract-7"><b>Did You 
Know?</b></span>How to read a car ad:</p>

but none of the following items.

My code looks like this:

...

<xsl:key name="bullet" match="oeb:p/node()" 
use="generate-id((../node()[1] | (preceding-sibling::oeb:span | 
self::oeb:span)[starts-with(.,'&amp;(!!char1!!);')])[last()])" />

...

<xsl:template match="oeb:p">

        <xsl:apply-templates select="node()[1] | 
span[starts-with(.,'&amp;(!!char1!!);')]" mode="start-group" />

it seems, that above the namespace prefix at the span element is missing:



span => oeb:span



Does this fix it?



Joerg



</xsl:template>

<xsl:template match="node()" mode="start-group">

        <p class="{../@class}">

                <xsl:copy-of select="key('bullet',generate-id())" />

        </p>

</xsl:template>

...

I changed the hard-wired class attribute in your version to capture the 
class attribute value in the parent <p>, but I tried leaving that out

entirely, and got the same results. I left out the <root> surrounding the 
<p> template because I have other things that will also appear

as siblings to the <p>. I also added some namespaces that I needed. 
Otherwise, I think I copied your suggestions exactly, unless there's

a typo in there somewhere that has eluded me. I seem to be getting all of 
my data coming through correctly, except for the missing list items.

What am I doing wrong? Thanks again.



At 10:03 AM 12/7/02, you wrote:



Hello Chris,



Chris Loschen wrote:



I have some mixed content paragraphs which I need to break up into 
multiple paragraphs, each one beginning with a specified string. See my 
sample input and desired output below. (I'll clean up some of the other 
cruft like the extra br elements later.) I tried to set up some 
templates (which I've included below) for this based on the example in 
Michael Kay's XSLT 2nd ed., p. 550. However, Mr. Kay's script was 
dealing with a pure string of #PCDATA and I've got nodes mixed in, so 
I'm getting an error that I cannot "convert #STRING to a NodeList!" (If 
I've misunderstood any of that, please let me know.) What am I doing 
wrong? Or do I need to totally rethink my approach, because 
"substring-before" and "substring-after" are meant to work on strings, 
not mixed content?




Yes! It's XML, nodes, elements, ... and not simply a text string.



If that's it, is there a way to accomplish what I  need to do?




It's a grouping problem.



Using this XSLT:



<xsl:key name="nodes" match="/p/node()" use="generate-id((../node()[1] |
    (preceding-sibling::span|self::span)[starts-with(.,
        '&amp;(!!char1!!);')])[last()])"/>

<xsl:template match="/p">

    <root>

        <xsl:apply-templates select="node()[1] | span[starts-with(., 
'&amp;(!!char1!!);')]" mode="start-group"/>

    </root>

</xsl:template>



<xsl:template match="node()" mode="start-group">
    <p class="extract-9">
        <xsl:copy-of select="key('nodes', generate-id())"/>
    </p>
</xsl:template>

I get an output, which looks at least similar to your desired output. 
The nodes are grouped by their preceding-sibling::span containing 
'char1'. If the "current" node itself is a 'char1'-span element, it must 
not use preceding-sibling, but itself. And because it seems to be 
possible, that the first node doesn't contain 'char1', you must access 
it by hand. This results in



(../node()[1] |
    (preceding-sibling::span | self::span)
        [starts-with(.,'&amp;(!!char1!!);')])
[last()]

Everything clear?



Regards,



Joerg



Thanks again for all your previous assistance, and thank you in advance 
for helping me with this one.



SAMPLE INPUT:

<p class="extract-9"><span class="extract-7"><b>Did You 
Know?</b></span>How to read a car ad:<span>&amp;(!!char1!!);</span> 
<i>Low mileage</i> means <i>the odometer doesn&rsquo;t work<br 
/></i><span>&amp;(!!char1!!);</span> <i>All original</i> means <i>needs 
new everything<br /></i><span>&amp;(!!char1!!);</span> <i>Health forces 
sale</i> means <i>I&rsquo;m sick of this car<br 
/></i><span>&amp;(!!char1!!);</span> <i>Must see</i> means <i>I 
won&rsquo;t put anything in writing<br 
/></i><span>&amp;(!!char1!!);</span> <i>Runs like a top</i> means 
<i>wobbles when driven slowly<br /></i><span>&amp;(!!char1!!);</span> 
<i>Mint</i> means <i>there&rsquo;s an old roll of Lifesavers under the 
seat<br /></i><span>&amp;(!!char1!!);</span> <i>Rare</i> means <i>most 
examples of this model fell apart long ago</i></p>

DESIRED OUTPUT:

<p class="extract-9"><span class="extract-7"><b>Did You 
Know?</b></span>How to read a car ad:</p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Low mileage</i> 
means <i>the odometer doesn&rsquo;t work<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>All original</i> 
means <i>needs new everything<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Health forces 
sale</i> means <i>I&rsquo;m sick of this car<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Must see</i> 
means <i>I won&rsquo;t put anything in writing<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Runs like a 
top</i> means <i>wobbles when driven slowly<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Mint</i> means 
<i>there&rsquo;s an old roll of Lifesavers under the seat<br /></i></p>

<p class="extract-9"><span>&amp;(!!char1!!);</span> <i>Rare</i> means 
<i>most examples of this model fell apart long ago</i></p>

<xsl-code snipped="true"/>



--Chris





XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

--Chris

---------------------------------------------------------------------------------------- 



Texterity ~ XML and PDF ePublishing Services

---------------------------------------------------------------------------------------- 



Chris Loschen, XML Developer
Texterity, Inc.
144 Turnpike Road
Southborough, MA 01772 USA
tel: +1.508.804.3033
fax: +1.508.804.3110
email: loschen@xxxxxxxxxxxxx
http://www.texterity.com/
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

--Chris



----------------------------------------------------------------------------------------

Texterity ~ XML and PDF ePublishing Services

----------------------------------------------------------------------------------------

Chris Loschen, XML Developer

Texterity, Inc.

144 Turnpike Road

Southborough, MA 01772 USA

tel: +1.508.804.3033

fax: +1.508.804.3110

email: loschen@xxxxxxxxxxxxx

http://www.texterity.com/ 




XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.