У меня есть XML-файл. Я сталкиваюсь с проблемой при вложении раздела level = "2" в section level = "1", а также хочу, чтобы следующий элемент-брат находился внутри раздела level 1., но я не могу его получить.
Входной XML
<section level="1" counter="yes">
<section level="2" counter="yes">
<title>Introduction</title>
</section>
<para>Started the campaign with reviewing the top procedures for men.</para>
<para>Gynecomastia (removal of breast tissue).</para>
<section level="2" counter="yes">
<title>Capturing the Wave</title>
</section>
<para>Our focus initially was to create robust website content.</para>
<para>Started the campaign with reviewing the top procedures for men.</para>
<section level="3" counter="yes">
<title>Our Approach: Build the Platform</title>
</section>
<para>Our criteria included that each service and landing page included at least 600 words</para>
<para>Our focus initially was to create robust website content.</para>
<section level="4" counter="yes">
<title>Capturing the Wave</title>
</section>
<para>Our content-first strategy, along with a mobile-responsive design</para>
<para>Our focus initially was to create robust website content.</para>
</section>
Выходной XML: -
<section level="1" counter="yes">
<section level="2" counter="yes">
<title>Introduction</title>
<para>Started the campaign with reviewing the top procedures for men.</para>
<para>Gynecomastia (removal of breast tissue).</para>
</section>
<section level="2" counter="yes">
<title>Capturing the Wave</title>
<para>Our focus initially was to create robust website content.</para>
<para>Started the campaign with reviewing the top procedures for men.</para>
<section level="3" counter="yes">
<title>Our Approach: Build the Platform</title>
<para>Our criteria included that each service and landing page included at least 600 words</para>
<para>Our focus initially was to create robust website content.</para>
<section level="4" counter="yes">
<title>Capturing the Wave</title>
<para>Our content-first strategy, along with a mobile-responsive design</para>
<para>Our focus initially was to create robust website content.</para>
</section>
</section>
</section>
</section>
И xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section[@level = '2']">
<xsl:element name="section">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="child::*"/>
<xsl:variable name="prev-level-value" select="."/>
<xsl:for-each select="following-sibling::*:section[@level = '3']">
<xsl:variable name="curr-level" select="@level - 1"/>
<xsl:variable name="curr-level-value" select="preceding-sibling::*:section[@level = $curr-level][1]"/>
<xsl:apply-templates select="self::*:section[@level = '3'][$curr-level-value = $prev-level-value]" mode="nested"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="section[@level = '3']" mode="nested">
<xsl:element name="section">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="child::*"/>
<xsl:variable name="prev-level-value" select="."/>
<xsl:for-each select="following-sibling::*:section[@level = '4']">
<xsl:variable name="curr-level" select="@level - 1"/>
<xsl:variable name="curr-level-value" select="preceding-sibling::*:section[@level = $curr-level][1]"/>
<xsl:apply-templates select="self::*:section[@level = '4'][$curr-level-value = $prev-level-value]" mode="nested"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="section[@level = '4']" mode="nested">
<xsl:element name="section">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="child::*"/>
<xsl:variable name="prev-level-value" select="."/>
<xsl:for-each select="following-sibling::*:section[@level = '5']">
<xsl:variable name="curr-level" select="@level - 1"/>
<xsl:variable name="curr-level-value" select="preceding-sibling::*:section[@level = $curr-level][1]"/>
<xsl:apply-templates select="self::*:section[@level = '5'][$curr-level-value = $prev-level-value]" mode="nested"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="section[@level = '5']" mode="nested">
<xsl:element name="section">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="child::*"/>
<xsl:variable name="prev-level-value" select="."/>
<xsl:for-each select="following-sibling::*:section[@level = '6']">
<xsl:variable name="curr-level" select="@level - 1"/>
<xsl:variable name="curr-level-value" select="preceding-sibling::*:section[@level = $curr-level][1]"/>
<xsl:apply-templates select="self::*:section[@level = '6'][$curr-level-value = $prev-level-value]" mode="nested"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="section[@level = '6']" mode="nested">
<xsl:element name="section">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="child::*"/>
<xsl:variable name="prev-level-value" select="."/>
<xsl:for-each select="following-sibling::*:section[@level = '7']">
<xsl:variable name="curr-level" select="@level - 1"/>
<xsl:variable name="curr-level-value" select="preceding-sibling::*:section[@level = $curr-level][1]"/>
<xsl:apply-templates select="self::*:section[@level = '7'][$curr-level-value = $prev-level-value]" mode="nested"/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Я запустил XSLT на входе xml, затем мой выводэто:
<section level="1" counter="yes">
<section level="2" counter="yes">
<title>Introduction</title>
</section>
<para>Started the campaign with reviewing the top procedures for men.</para>
<para>Gynecomastia (removal of breast tissue).</para>
<section level="2" counter="yes">
<title>Capturing the Wave</title>
<section level="3" counter="yes">
<title>Our Approach: Build the Platform</title>
<section level="4" counter="yes">
<title>Capturing the Wave</title>
</section>
</section>
</section>
<para>Our focus initially was to create robust website content.</para>
<para>Started the campaign with reviewing the top procedures for men.</para>
<section level="3" counter="yes">
<title>Our Approach: Build the Platform</title>
</section>
<para>Our criteria included that each service and landing page included at least 600 words</para>
<para>Our focus initially was to create robust website content.</para>
<section level="4" counter="yes">
<title>Capturing the Wave</title>
</section>
<para>Our content-first strategy, along with a mobile-responsive design</para>
<para>Our focus initially was to create robust website content.</para>
</section>