Re: [xsl] HTML within an element

Subject: Re: [xsl] HTML within an element
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 23 Jul 2001 18:10:38 +0100
Hi Nate,

> I am still having problems grabbing the text of an
> element if it contains HTML. i.e.
> External DTD declaration:
> <!ELEMENT SubSectionText (#PCDATA)>

I suspect that you've copied the wrong element declaration, and that
you also have a declaration that looks like:

<!ELEMENT SectionText (#PCDATA)>

This declares that the SectionText elements holds text, and only text.
However, in your XML:

> XML:
> <Section Title="Mission"
> URL="StrategicVision.htm#mission">
>     <SectionText>Conduct basic and applied research to
> support human 
> exploration of <em>space</em> and to take advantage of
> the <strong>space environment</strong> as a 
> laboratory for <a href="test.htm">scientific</a>,
> technological, and commercial 
> research.</SectionText>
> </Section>

the SectionText element has other elements within it, such as em,
strong and a elements. You need to change the content model in your
DTD to:

<!ELEMENT SectionText (#PCDATA | em | strong | a)*>

to allow these elements to appear according to the DTD. Doing so will
prevent complains such as "Element invalid according to DTD. Expecting
#PCDATA". When you get an error message like that it means that the
XML document that you're using doesn't match the DTD that's associated
with the document. Changing the XSLT will have no effect on whether
the XML document parses OK or not, so you need to get that working, by
changing the XML document and/or the DTD, first, before you start
changing the XSLT.

If you don't care about the XML document adhering to a DTD, then don't
have a DOCTYPE declaration in your XML document. If you just want to
allow the SectionText element to contain anything, then change the DTD
to hold the following:

<!ELEMENT SectionText ANY>

As far as your XSLT goes, the following template:

> <xsl:template match="SectionText">
> <xsl:if test="string-length(text()) > 0">
> <p><xsl:copy-of select="." /></p></xsl:if>
> </xsl:template>

is probably the one that you want, although the test for the xsl:if is
a little strange. Here you're testing whether the string length of the
first text node child of the SectionText element is greater than 0. I
suspect that you want to test whether the SectionText element contains
any text, in which case the simplest thing to do is:

<xsl:template match="SectionText">
  <xsl:if test="string()">
    <p><xsl:copy-of select="." /></p>



Jeni Tennison

 XSL-List info and archive:

Current Thread