Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: XSLT - select nodes without specific children? [Thread Next] Re: XSLT - select nodes without specific children?To: NULL Date: 6/2/2004 2:55:00 PM A more generalized solution:
if all the "div" nodes are at the same level in the xml file, the following
solution can be used:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<text>
<xsl:for-each
select="txtSectionBody/div[not(descendant::table)]">
<Paragraph>
<xsl:copy-of select="./*"/>
</Paragraph></xsl:for-each>
</text>
<xsl:for-each select="txtSectionBody/div[descendant::table]">
<xsl:copy-of select="./*/table"/></xsl:for-each>
</xsl:template>
</xsl:stylesheet>
"Chris Barber" <chris@b...> wrote in message
news:en6HReoREHA.2408@t......
> You might be better off with templates:
>
> This XSLT:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="xml" omit-xml-declaration="yes"/>
> <xsl:template match="/">
> <html>
> <body>
> <xsl:apply-templates/>
> </body>
> </html>
> </xsl:template>
> <!-- Match a DIV that doesn't contain a table element as a direct
child -->
> <xsl:template match="div[not(span/table)]">
> <p>
> <p>DIV
> <b>not wrapping</b> a table
> </p>
> <!-- Apply the template that will process a table -->
> <!--<xsl:apply-templates select="span/table"/>-->
> <!-- Alternatively to copy in wholesale -->
> <xsl:copy-of select="./*"/>
> </p>
> </xsl:template>
> <!-- Match a DIV that does contain a table element as a child -->
> <xsl:template match="div[span/table]">
> <p>
> <p>DIV
> <b>wrapping</b> a table
> </p>
> <!-- Apply the template that will process a table -->
> <!--<xsl:apply-templates select="span/table"/>-->
> <!-- Alternatively to copy in wholesale -->
> <xsl:copy-of select="span/table"/>
> </p>
> </xsl:template>
> </xsl:stylesheet>
>
> Produces:
>
> <html>
> <body>
> <p>
> <p>DIV
> <b>not wrapping</b> a table
> </p>
> <span>
> Text Content
> </span>
> </p>
> <p>
> <p>DIV
> <b>wrapping</b> a table
> </p>
> <table>
> <tbody vAlign="top">
> <tr>
> <td>
> <div>
> <font style="FONT-SIZE: 11pt" size="+0">
> <strong>Dose (mg)</strong>
> </font>
> </div>
> </td>
> </tr>
> <tr>
> <td>
> <div>
> <font style="FONT-SIZE: 11pt">
> 250
> </font>
> </div>
> </td>
> </tr>
> </tbody>
> </table>
> </p>
> <p>
> <p>DIV
> <b>not wrapping</b> a table
> </p>
> <font face="undefined">
> <span style="FONT-SIZE: 11pt">Various text content</span>
> </font>
> </p>
> <p>
> <p>DIV
> <b>not wrapping</b> a table
> </p>
> <font face="undefined">
> <span style="FONT-SIZE: 11pt">More text content</span>
> </font>
> </p>
> </body>
> </html>
>
> Which should demonstrate what you want?
>
> Chris.
>
> "Philo" <philojfarnsworth@m...> wrote in message
> news:Xns94F9882C91799msphilo@2......
> How do I select all <div> tags except those which contain a <table> tag
> somewhere within them?
>
> Example XML:
>
> <********************** sample input ***********************>
>
> <txtSectionBody>
> <div>
> <span>
> Text Content
> </span>
> </div>
> <div>
> <span>
> <table>
> <tbody vAlign="top">
> <tr>
> <td>
> <div>
> <font style="FONT-SIZE: 11pt"
> size="+0">
> <strong>Dose (mg)</strong>
> </font>
> </div>
> </td>
> </tr>
> <tr>
> <td>
> <div>
> <font style="FONT-SIZE: 11pt">
> 250
> </font>
> </div>
> </td>
> </tr>
> </tbody>
> </table>
> </span>
> </div>
> <div>
> <font face="undefined">
> <span style="FONT-SIZE: 11pt">Various text content</span>
> </font>
> </div>
> <div>
> <font face="undefined">
> <span style="FONT-SIZE: 11pt">More text content</span>
> </font>
> </div>
> </txtSectionBody>
>
> </********************** sample input ***********************>
>
> Which I need to transform to
> <text>
> <paragraph>
> Text content
> </paragraph>
> <table>
> <tr>
> <td>Dose (mg)</td>
> </tr>
> <tr>
> <td>250</td>
> </tr>
> </table>
> <paragraph>
> Various text content
> </paragraph>
> <paragraph>
> More text content
> </paragraph>
> </text>
>
> So I need to select out the div's that *don't* wrap a table and
> transform them to paragraph tags, then grab the table out of the div tag
> and put it as a child of the document.
>
> I'm trying this:
> <text>
> <xsl:for-each select="txtSectionBody">
> <xsl:for-each select="xhtml:div[not(self::table)]">
> <paragraph>
> <xsl:value-of select="."/>
> </paragraph>
> </xsl:for-each>
> <xsl:for-each select="xhtml:div/span/table">
> <table>
> <xsl:for-each select="tbody/tr">
> <tr>
> <xsl:for-each select="td">
> <td>
> <xsl:value-of select="."/>
> </td>
> </xsl:for-each>
> </tr>
> </xsl:for-each>
> </table>
> </xsl:for-each>
> </xsl:for-each>
> </text>
>
> Help?
>
> Philo
> (email is first name, middle initial "j", last name. Remove the last
> name to reply)
>
>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
