Create child nodes from sibling nodes until another sibling occurs

Hi, does anyone know what xsl will look like to convert this XML. Maybe N nte after pid and N nte after pv1. The structure is guaranteed by the fact that all nte following pid belong to pid and all nte following pv1 belong to pv1.

From:

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

at:

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

Thank!

+3
source share
1 answer

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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 to the provided XML document (corrected for correctness) :

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

creates the desired, correct result :

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

Source: https://habr.com/ru/post/1749675/


All Articles