[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

[no subject]

[no subject]
<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@xxxxxxxx]
Sent: Monday, October 01, 2007 10:41 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.