xsl-list
[Top] [All Lists]

Re: [xsl] Grouping to Separate Out Block Level Elements from Mixed Content

2022-02-05 09:50:58
Hi Don,



I recently had to solve the opposite problem – inline content in <li> *not* 
being wrapped in <p>. But it’s a similar problem – wrap some stuff but not 
other stuff. I’m attaching the code I used.



The basic approach is to use helper functions to identify elements as 
block/inline, then <for-each-group group-adjacent=...> to wrap inline element 
sequences in the current element but not block element sequences.



Note the following:



  *   My code has some heuristics to wrap <indexterm> elements only when 
they’re along with real inline content; you might not need that.
  *   My code uses <xsl:next-match/> because it’s extracted from a larger XSLT 
file that does other stuff; you might not need that.
  *   My helper functions use a series of [self::A or self::B or …] that is a 
bit inelegant. I should rewrite it to use the template-based approach suggested 
in the recent “Using node-set variables in predicates” discussion on this list, 
as I now prefer that style for element-class helper functions.
  *   Because you’re solving the reverse problem (breaking content out of an 
existing context rather than wrapping it in a new context), be sure to copy any 
<p @props/@audience/@product/...> attributes to block elements you push out of 
the <p> context.



My template matches like this:



<xsl:template match="*[mine:disallow-inline(.)][node()[mine:is-inline(.)]]">

  ...

</xsl:template>



which says, “match any element that contains inline content (is-inline()), but 
*I* don’t want it to (disallow-inline()). The important distinction is, 
disallow-inline() means *I* don’t want it to contain inline content; it has 
nothing to do with what DITA itself allows.



So you’ll probably need something more like this:



<xsl:template match="*[mine:disallow-block(.)][node()[mine:is-block(.)]]">

  ...

</xsl:template>



As for what to include in the lists, I used the content_mode.pl script at



https://github.com/chrispy-snps/DITA-plugin-utilities



to dump out our content models, then did some analysis based on which elements 
are permitted in <body>, which elements are permitted in <p>, and so on. That 
was a manual effort; maybe some day I’ll script it up so I can regenerate the 
element lists as adjust our specializations.



  *   Chris




--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

Attachment: wrap-inline-content-in-p.xsl
Description: wrap-inline-content-in-p.xsl

<Prev in Thread] Current Thread [Next in Thread>