elementos de separación se mueven hacia arriba en la jerarquía XML
Pregunta
Tengo un documento XML con separadores en el fondo de la jerarquía.
<A>
<B>
<C id='1'/>
<separator/>
<C id='2'/>
</B>
<B>
<C id='3'/>
<separator/>
</B>
<B>
<C id='4'/>
</B>
</A>
Quiero mover los separadores hacia arriba, manteniendo los elementos en orden. Por lo que la salida deseada es
<A>
<B>
<C id='1'/>
</B>
</A>
<separator/>
<A>
<B>
<C id='2'/>
</B>
<B>
<C id='3'/>
</B>
</A>
<separator/>
<A>
<B>
<C id='4'/>
</B>
</A>
¿Cómo puede hacerse utilizando XSLT sólo el 1,0? ¿Se puede hacer sin for-each
, utilizando partido plantilla sólo?
ACTUALIZACIÓN: De hecho, me dieron 4 respuestas brillante de diferentes niveles de generalidad, gracias, chicos.
Solución
Esta hoja de estilo:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kCByFollSep" match="C"
use="generate-id(following::separator[1])"/>
<xsl:template match="A">
<xsl:for-each select="B/separator|B[last()]/*[last()]">
<A>
<xsl:apply-templates
select="key('kCByFollSep',
substring(generate-id(),
1 div boolean(self::separator)))"/>
</A>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="C">
<B>
<xsl:copy-of select="."/>
</B>
</xsl:template>
</xsl:stylesheet>
Salida:
<A>
<B>
<C id="1" />
</B>
</A>
<separator />
<A>
<B>
<C id="2" />
</B>
<B>
<C id="3" />
</B>
</A>
<separator />
<A>
<B>
<C id="4" />
</B>
</A>
Nota: . La agrupación siguiendo separator
, añadiendo último elemento tercer nivel de C
Posible sin seguir separator
Editar : Más estilo tirón, más esquema agnóstico, esta hoja de estilo:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kCByFollSep" match="C"
use="generate-id(following::separator[1])"/>
<xsl:template match="text()"/>
<xsl:template match="separator|*[not(*)][not(following::*)]">
<A>
<xsl:apply-templates
select="key('kCByFollSep',
substring(generate-id(),
1 div boolean(self::separator)))"
mode="output"/>
</A>
<xsl:copy-of select="self::separator"/>
</xsl:template>
<xsl:template match="C" mode="output">
<B>
<xsl:copy-of select="."/>
</B>
</xsl:template>
</xsl:stylesheet>
EDIT 2 : solución más general (! Una cosa que no hace confianza, ja)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pRemains"/>
<xsl:copy>
<xsl:apply-templates select="node()[descendant-or-self::node()
[not(self::separator)]
[count(following::separator)
= $pRemains]
][1]|@*">
<xsl:with-param name="pRemains" select="$pRemains"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()
[descendant-or-self::node()
[not(self::separator)]
[count(following::separator)
= $pRemains]
][1]">
<xsl:with-param name="pRemains" select="$pRemains"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vRemains" select="last()-position()"/>
<xsl:for-each select="$vCurrent">
<xsl:copy>
<xsl:apply-templates
select="node()[descendant::node()
[not(self::separator)]
[count(following::separator)
= $vRemains]
][1]">
<xsl:with-param name="pRemains" select="$vRemains"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
Nota: : En su mayoría un recorrido de grano fino. Una regla de jerarquía baja (en este caso el elemento raíz) copias de sí mismo y el separador (nodo ficticio para el último grupo sin seguir separador) que pasa separadores resto para procesar primer hijo con suficientes separadores siguientes a proceso. Una regla de la identidad de recorrido de grano fino modificado, copiándose y de nuevo el procesamiento primer hijo y siguiendo hermano con suficientes separadores siguientes a proceso. Por fin, una regla separador de romper el proceso.
Editar 3 : Otra solución más general, ahora con la regla recursiva identidad
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
use="generate-id((descendant::separator|following::separator)[1])"/>
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pGroup"/>
<xsl:copy>
<xsl:apply-templates
select="node()[descendant-or-self::node()[count(.|$pGroup)
= count($pGroup)]]|@*">
<xsl:with-param name="pGroup" select="$pGroup"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vGroup"
select="key('kNodeByFolSep',generate-id(self::separator))"/>
<xsl:for-each select="$vCurrent">
<xsl:call-template name="identity">
<xsl:with-param name="pGroup" select="$vGroup"/>
</xsl:call-template>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
Editar 4 :. Ahora lo mismo que antes, pero con prueba clave en lugar de intersección de conjunto de nodos
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
use="concat(generate-id(),'+',
generate-id((descendant::separator|
following::separator)[1]))"/>
<xsl:template match="node()|@*" name="identity">
<xsl:param name="pSeparator"/>
<xsl:copy>
<xsl:apply-templates
select="@*|node()[descendant-or-self::node()
[key('kNodeByFolSep',
concat(generate-id(),
'+',
$pSeparator))]]">
<xsl:with-param name="pSeparator" select="$pSeparator"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:variable name="vCurrent" select="."/>
<xsl:for-each select="descendant::separator|node()[last()]">
<xsl:variable name="vSeparator"
select="generate-id(self::separator)"/>
<xsl:for-each select="$vCurrent">
<xsl:call-template name="identity">
<xsl:with-param name="pSeparator" select="$vSeparator"/>
</xsl:call-template>
</xsl:for-each>
<xsl:copy-of select="self::separator"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="separator"/>
</xsl:stylesheet>
Otros consejos
Esta transformación :
<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="kFollowing" match="C"
use="generate-id(preceding::separator[1])"/>
<xsl:template match="/">
<xsl:apply-templates select="*/*/separator"/>
</xsl:template>
<xsl:template match="separator" name="commonSep">
<separator/>
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="ancestor::*"/>
<xsl:with-param name="pLeaves"
select="key('kFollowing', generate-id())"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="separator[not(preceding::separator)]">
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="ancestor::*"/>
<xsl:with-param name="pLeaves"
select="key('kFollowing', '')"/>
</xsl:call-template>
<xsl:call-template name="commonSep"/>
</xsl:template>
<xsl:template name="genAncestors">
<xsl:param name="pAncs" select="ancestor::*"/>
<xsl:param name="pLeaves" select="."/>
<xsl:choose>
<xsl:when test="not($pAncs[2])">
<xsl:apply-templates select="$pLeaves" mode="gen"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$pAncs[1]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:call-template name="genAncestors">
<xsl:with-param name="pAncs" select="$pAncs[position()>1]"/>
<xsl:with-param name="pLeaves" select="$pLeaves"/>
</xsl:call-template>
</xsl:copy>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="C" mode="gen">
<xsl:variable name="vCur" select="."/>
<xsl:for-each select="..">
<xsl:copy>
<xsl:copy-of select="@*|$vCur"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
cuando se aplica sobre el documento XML proporcionado
<A>
<B>
<C id='1'/>
<separator/>
<C id='2'/>
</B>
<B>
<C id='3'/>
<separator/>
</B>
<B>
<C id='4'/>
</B>
</A>
produce el deseado, resultado correcto
<A>
<B>
<C id="1"/>
</B>
</A>
<separator/>
<A>
<B>
<C id="2"/>
</B>
<B>
<C id="3"/>
</B>
</A>
<separator/>
<A>
<B>
<C id="4"/>
</B>
</A>