tags:

views:

48

answers:

1

Hi does anyone know what the xsl would look like to transform this XML. There can be N nte's after pid and N nte's after pv1. The structure is guaranteed in that all nte that follows pid belongs to pid and all nte following pv1 belongs to pv1.

From:

<pid>
</pid>
<nte> 
  <nte-1>1</nte-1>
  <nte-3>Note 1</nte-1>
</nte>
<nte></nte>
<pv1></pv1>
<nte>
</nte>

into:

<pid>
  <nte> 
    <nte-1>1</nte-1>
    <nte-3>Note 1</nte-1>
  </nte>
  <nte>
  </nte>
</pid>
<pv1>
  <nte>
  </nte>
</pv1>

Thanks!

+3  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kLogicalChildren" match="nte"
  use="generate-id(preceding-sibling::*
                        [self::pid or self::pv1]
                         [1])"/>

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

 <xsl:template match="pid|pv1">
  <xsl:copy>
    <xsl:copy-of select="@*"/>

    <xsl:copy-of select=
    "key('kLogicalChildren', generate-id())"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="nte"/>
</xsl:stylesheet>

when applied on the provided XML document (corrected to be well-formed):

<t>
    <pid></pid>
    <nte>
        <nte-1>1</nte-1>
        <nte-3>Note 1</nte-3>
    </nte>
    <nte></nte>
    <pv1></pv1>
    <nte></nte>
</t>

produces the wanted, correct result:

<t>
    <pid>
        <nte>
            <nte-1>1</nte-1>
            <nte-3>Note 1</nte-3>
        </nte>
        <nte/>
    </pid>
    <pv1>
        <nte/>
    </pv1>
</t>
Dimitre Novatchev
Works like a charm! Thanks!