Re: [xsl] flattening and re-ordering nested notes

Subject: Re: [xsl] flattening and re-ordering nested notes
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 18 Aug 2008 17:06:42 +0100
>> Put in the effort to create an example input and required output and
>> someone might post the code to turn that input into that output...
>
>
> Thanks in advance if you can help:
>
> http://pastie.org/254986


So the sample input is:


<publication pub-id="lf1454">
<document>
<!-- many heads, divs, chapters, sections, etc. in here; div:para
     fragment which follows might be the child of another div, and that
     hierarchy cannot change -->
<div type="para">
  <p>
    To an authority of <hi rend="ital">such</hi> a weight, I could add the
    sanction of Cantillon, whose <!-- content elided for brevity -->and
    transient power<ref type="footnote" target="lf1454_footnote_nt070">*</ref>
    <note type="footnote" id="lf1454_footnote_nt070">
      <label id="lf1454_label_073">* </label>
      <p>
        Although it <hi rend="ital">may,</hi> in some degree, prove foreign to
        the subject, it does not seem absolutely improper to introduce an
        observation, intitled to the notice of the reader, and which appears to
        have escaped the attention of <hi rend="ital">most</hi> writers,
        Professor Smith<ref type="footnote"
target="lf1454_footnote_nt071">&#x2020;</ref>
        <note type="footnote" id="lf1454_footnote_nt071">
          <label id="lf1454_label_074">&#x2020; </label>
          <p>
            See &#x201C;An Inquiry into the Nature and Causes of the Wealth of
            Nations.&#x201D;
          </p>
        </note>
        (a politician of equal depth and judgment, to whom society owes
        <hi rend="ital">many</hi> obligations) and Mr. Hume excepted: Mr. Hume,
        who, <!-- content elided for brevity --> &#x201D;
      </p>
    </note>
    . This opulence, on which it plumes itself so much, is soon dispersed and
    vanishes; <!-- content elided for brevity --> things.
  </p>
</div>
<!-- many heads, divs, chapters, sections, etc. to follow -->
</document>
</publication>



The required output is:



<publication pub-id="lf1454">
<document>
<!-- many heads, divs, chapters, sections, etc. in here; div:para
     fragment which follows might be the child of another div, and that
     hierarchy cannot change -->
<div type="para">
  <p>
    To an authority of <hi rend="ital">such</hi> a weight, I could add the
    sanction of Cantillon, whose <!-- content elided for brevity -->and
    transient power<ref type="footnote" target="lf1454_footnote_nt070">*</ref>.
    This opulence, on which it plumes itself so much, is soon dispersed and
    vanishes; <!-- content elided for brevity --> things.
  </p>
</div>
<!-- many heads, divs, chapters, sections, etc. to follow -->
<note type="footnote" id="lf1454_footnote_nt070">
  <label id="lf1454_label_073">* </label>
  <p>
    Although it <hi rend="ital">may,</hi> in some degree, prove foreign to the
    subject, it does not seem absolutely improper to introduce an observation,
    intitled to the notice of the reader, and which appears to have escaped the
    attention of <hi rend="ital">most</hi> writers, Professor Smith
    <ref type="footnote" target="lf1454_footnote_nt071">&#x2020;</ref> (a
    politician of equal depth and judgment, to whom society owes
    <hi rend="ital">many</hi> obligations) and Mr. Hume excepted: Mr.
Hume, who,
    <!-- content elided for brevity --> &#x201D;
  </p>
</note>
<note type="footnote" id="lf1454_footnote_nt071">
  <label id="lf1454_label_074">&#x2020; </label>
  <p>
    See &#x201C;An Inquiry into the Nature and Causes of the Wealth of
    Nations.&#x201D;
  </p>
</note>
</document>
</publication>


Better than nothing, but both can be shortened down to:

Input:

<publication>
  <document>
    <div>
      <note id="note1">
        <note id="note2"/>
       </note>
    </div>
  </document>
</publication>

Output:

<publication>
  <document>
    <div/>
    <note id="note1"/>
    <note id="note2"/>
  </document>
</publication>

...which demonstrates the same problem, but with all unrelated
information removed.  This sort of small, complete example is what you
should be aiming for when posting a question to a mailing list.

Here's a transform that should do what you need:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    exclude-result-prefixes="xs">

<xsl:template match="@*|node()" mode="#all">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="note"/>

<xsl:template match="document">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
	<xsl:apply-templates select="//note" mode="notes"/>
</xsl:template>

</xsl:stylesheet>

If you are stuck with 1.0 it will take a bit more effort because it
doesn't have mode="#all" (you just have to copy the identity template
and put it in the mode="notes")

-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread