xsl-list
[Top] [All Lists]

Re: [xsl] Wrap an entire element if it starts with text

2011-11-22 19:29:33
Your approach is inside out.

At 2011-11-22 20:06 -0500, Rick Quatro wrote:
  <xsl:template match="//node()[parent::entry[not(*)][.!=' ']]">
    <xsl:element name="p">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:element>
  </xsl:template>

You need to take action at the entry, not at the nodes below the entry:

  <xsl:template match="entry[text()[normalize-space(.)!='']]">
    <xsl:copy>
       <xsl:apply-templates select="@*"/>
       <p>
         <xsl:apply-templates select="node()"/>
       </p>
    </xsl:copy>
  </xsl:template>

The above will match any entry that has at least one non-white-space-only text node child.

Interestingly, earlier today on this list I mentioned that I very rarely ever have to address text nodes, and yet here is such an example. Your criterion is that there is a non-empty text node child that isn't wrapped with a paragraph, so we have to specifically look at text node children.

I hope this helps.

. . . . . . . . . . . Ken

At 2011-11-22 20:06 -0500, Rick Quatro wrote:
I have a series of <entry> elements whose entire contents I want to wrap in
a <p> element.

For example, here is before:

<entry>This is a raw entry that needs a p element.</entry>

Here is after:

<entry><p>This is a raw entry that needs a p element.</p></entry>

I want to ignore <entry> elements that already have a top-level child, like
this:

<entry><note>I don't want to add p to these.</note><entry>

I also want to ignore <entry> elements that just consist of a space, like
this:

<entry> </entry>

With my stylesheet and XPath statement below, everything works fine.
However, it doesn't handle elements like these, that also need to have their
contents wrapped:

<entry>This contains a nested <note>child</note> and I need these wrapped as
well.</entry>

I want to get this:

<entry><p>This contains a nested <note>child</note> and I need these wrapped
as well.</p></entry>

Any pointers or suggestions would be appreciated. Thank you very much.

Rick

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
  <xsl:template match="node() | @*">
    <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="//node()[parent::entry[not(*)][.!=' ']]">
    <xsl:element name="p">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>


--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/t37DVX
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>