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

Subject: Re: [xsl] flattening and re-ordering nested notes
From: Walter Lee Davis <waltd@xxxxxxxxxxxx>
Date: Mon, 18 Aug 2008 13:37:35 -0400
On Aug 18, 2008, at 12:06 PM, Andrew Welch wrote:

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:

[snip]
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.

Thanks for the example, I'll try to remember that in the future. I was hoping to spare the bulk of the list having to slog through the whole thing, hence the use of pastie. I did initially post something this brief, but was asked for more detail, hence the additional content (really quite a lot shortened from the original 340K document).



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")

I am indeed stuck in 1.0, so just to clarify, do you mean that I need to create a new template, mode="notes" and put it at the same level as the identity template, and then remove the mode="#all" bit from the identity template?




-- Andrew Welch


Thanks very much for your help, this looks extremely promising.

Walter

Current Thread