xsl-list
[Top] [All Lists]

RE: [xsl] iterate through nodes and determine output by node type

2007-10-01 09:27:03
Since you're implying that any text _and_ HTML inline elements not
surrounded in a block-level tag should be wrapped in a <p> tag, it looks
like you're going to have to define what should be considered
block-level and what should be considered inline-level in your
stylesheet. (Pain in the neck.)

From there, you'll want something like the following, which looks for
any run of the same type of node and wraps it in a parent element:


<html:elements>
  <inline name=""/> <!-- this will match text nodes; watch out for
comments and whitespace nodes -->
  <inline name="a"/>
  <inline name="abbr"/>
  <inline name="acronym"/>
  <!-- ... -->
  <inline name="strong"/>
  <!-- ad nauseum... you'll have to fill in the rest here -->
</html:elements>

<!-- since HTML elements aren't case-sensitive,
     you'll probably need to use translate(., 'ABC...', 'abc...')
     for robustness here, eventually -->
<xsl:template match="//body/node()[name() =
document('')/html:elements/inline/@name]">
  <xsl:if test="not(name(preceding-sibling::node()[1]) =
document('')/html:elements/inline/@name)">
    <p>
      <xsl:apply-templates select="." mode="inline-run"/>
    </p>
  </xsl:if>
</xsl:template>

<xsl:template match="node()" mode="inline-run">
  <xsl:copy-of select="."/>
  <xsl:apply-templates select="following-sibling::node[1][name() =
document('')/html:elements/inline/@name]" mode="inline-run"/>
</xsl:template>



The document('')/html:elements/inline/@name part is a mouthful, but it
just happens that the test for the type of node you're looking for is
irritating to write. In general, the test for the initial element in the
run, coupled with a modal template to copy and apply-templates to the
next matching node in a row, will give you the grouped wrap you're
looking for. (Reversing is easy in this scenario too, just swap the
positions of <copy-of/> and <apply-templates/> in the modal template.)


~ Scott

-----Original Message-----
From: Mario Madunic [mailto:hajduk(_at_)imag(_dot_)net] 
Sent: Monday, October 01, 2007 10:41 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] iterate through nodes and determine output by node type

I'd like to test all child elements of a body tag and wrap any text
nodes (that
can contain child elements itself)in a p tag and <apply-templates /> to
any
element nodes.

Using XSLT 1.1

Sample input

<body>
  blah blah blah <strong>blah blah blah</strong>
  <p>a paragraph...</p>
  <p>a paragraph...</p>
  <table>
    <tr>
      <td>content goes here</td>
      <td>content goes here</td>
    <tr>
  </table>
</body>

sample output

<body>
  <p>blah blah blah <strong>blah blah blah</strong></p>
  <p>a paragraph...</p>
  <p>a paragraph...</p>
  <table>
    <tr>
      <td>content goes here</td>
      <td>content goes here</td>
    <tr>
  </table>
</body>

Sample code

<xsl:template match="//body">
  <body>
    <xsl:for-each select="child::*">
      <xsl:choose>
        <xsl:when test="text()">
          <p id="{generate-id()}"><xsl:apply-templates /></p>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates />
        </xsl:otherwise>
      </xsl:choose>                                     
    </xsl:for-each>
  </body>
</xsl:template>

Thanks

Marijan (Mario) Madunic

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--