RE: [xsl] Re: Restrict Mixed Content

Subject: RE: [xsl] Re: Restrict Mixed Content
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 11 Jan 2005 12:03:04 -0500
At 11:33 AM 1/11/2005, 'twas written:
> The source is coming from MS Word documents.  I only want to be
> able to use
> data such as bullets, lists, and tables.  All other elements I
> would like to
> ignore like graphs, images, etc.

Something like this...

        <!-- ignore everything -->
        <xsl:template match="*" />

        <!-- ...except for elements X, Y and Z -->
        <xsl:template match="X|Y|Z">
          <xsl:copy>
                <xsl:apply-templates mode="copy" select="*|*@" />
        <xsl:copy>
        </xsl:template>

        <xsl:template match="*|*@|text()" mode="copy" />
           <xsl:copy/>
        <xsl:template>

Watch out ... on this example:


<Q>
  <X>
    ... stuff ...
  </X>
  <Y>
    ... stuff ...
  </Y>
  <Z>
    ... stuff ...
  </Z>
</Q>

the above stylesheet would result in an empty document (nothing would be copied).

You could debug it by switching

<!-- ignore everything -->
<xsl:template match="*" />

to

<!-- by default, pass everything without copying
     (though since this template is built in it doesn't
      have to be included literally) -->
<xsl:template match="*">
  <xsl:apply-templates/>
</xsl:template>

<!-- by default, suppress text nodes
     (wanted text nodes are picked up in 'copy' mode -->
<xsl:template match="text()"/>

and also tweak

<xsl:template match="X|Y|Z">
  <xsl:copy>
    <xsl:apply-templates mode="copy" select="*|*@" />
  <xsl:copy>
</xsl:template>

to say <xsl:apply-templates mode="copy" select="node()|@*"/> -- since by selecting "*|@*" you'll skip the text nodes. (Or just say plain xsl:apply-templates mode="copy" and add an <xsl:copy-of select="@*"/> ahead of it to grab the attributes.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread