Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: Mario Madunic <hajduk@-------->
To:
Date: 10/1/2007 4:37:00 PM
Thanks Scott,

Would not have thought of this solution. I'll give it a try and post the results.

Marijan (Mario) Madunic

Quoting Scott Trenda <Scott.Trenda@xxxxxxxx>:

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


transparent
Print
Mail
Like It
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.

.
.

transparent

transparent