Re: [xsl] Display text and all the following nodes except <anchor> and <pb>

Subject: Re: [xsl] Display text and all the following nodes except <anchor> and <pb>
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Thu, 18 Sep 2008 03:58:55 -0400
Hiya! Won't surprise me at all if this question fits the catagory of
"been answered before if only one could easily find it".


> Can anyone help me to get the desire output!!! I want all the 
> text/following element except <pb> and <anchor> within <h1>

Given your sample input and desired output, I'm presuming you mean
you want all the elements except <anchor> and <bo> wihin <h1>, but
still want <anchor> and <pb> when they are outside of <h1>.

But it would help a lot if your input were well-formed:

> INPUT ...
> I<sCap>NFANTRY</sCap><anchor id="J">anchor</anchor><pb n="2"/>2</pb>
                                                              ^   ^

Moreover, I hope this input document isn't intended to be TEI. If so,
I'd be happy to talk about it off-list.

But to the point, the following console listing includes an XSLT 1.0
stylesheet that I think does everything you requestsed. However, this
doesn't do things *how* I think you requested. The ending <anchor> is
not processed as something following an <h1>, but rather as
something, anything, that is not inside an <h1>. I hope this does the
trick for you, or at least gives you an idea of how to do what you
want. Others on this list may, of course, be able to provide
improvements to my code.

---------
(518) Plod-2 /tmp @ 03:49:29 ->cat Untitled2.xml # INPUT FILE
<?xml version="1.0" encoding="UTF-8"?>
<duck>
  <head>O<sCap>FFICERS</sCap> of <person>the</person>
    R<sCap>EGIMENT</sCap> of I<sCap>NFANTRY</sCap><anchor id="J"
      >anchor</anchor><pb n="2">2</pb>
  </head>
  <anchor id="c">anchor1</anchor>
</duck>
(519) Plod-2 /tmp @ 03:49:42 ->
(519) Plod-2 /tmp @ 03:49:47 ->cat Untitled1.xsl # XSLT FILE
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <!-- match root, process its children -->
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  
  <!-- identity transform of anything not otherwise matched below -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!-- process <h1> specially: ignore child <anchor> or <pb> -->
  <xsl:template match="h1">
    <xsl:copy>
      <xsl:apply-templates
        select="@*
          |*[not(name(.)='anchor' or name(.)='pb')]
          |text()
          |comment()
          |processing-instruction()"
      />
    </xsl:copy>
  </xsl:template>
  
  <!-- phrase-level elements become <span> -->
  <xsl:template match="sCap|person">
    <xsl:element name="span">
      <xsl:attribute name="class"><xsl:value-of select="name(.)"/></xsl:attribute>
      <xsl:apply-templates/> <!-- presume input has no span= attribute -->
    </xsl:element>
  </xsl:template>
  
  <!-- <anchor>s become <a>s -->
  <xsl:template match="anchor">
    <xsl:element name="a">
      <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
      <xsl:apply-templates/> <!-- presume input has no id= attribute -->
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>
(520) Plod-2 /tmp @ 03:49:58 ->
(520) Plod-2 /tmp @ 03:49:58 ->xsltproc Untitled1.xsl Untitled2.xml # OUTPUT
<?xml version="1.0"?>
<duck>
  <head>O<span class="sCap">FFICERS</span> of <span class="person">the</span>
    R<span class="sCap">EGIMENT</span> of I<span class="sCap">NFANTRY</span><a name="J">anchor</a><pb n="2">2</pb>
  </head>
  <a name="c">anchor1</a>
</duck>
(521) Plod-2 /tmp @ 03:50:11 ->
---------

Current Thread