Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在xslt中创建嵌套循环_Xml_Xslt 2.0 - Fatal编程技术网

Xml 在xslt中创建嵌套循环

Xml 在xslt中创建嵌套循环,xml,xslt-2.0,Xml,Xslt 2.0,我有一个XML文件,当在section level=“1”中嵌套section level=“2”时,我面临一个问题,并且还希望下面的同级段落应该位于section level=“1”中。但我无法得到它 输入XML <section level="1" counter="yes"> <section level="2" counter="yes"> <title>Introduction</title> </s

我有一个XML文件,当在section level=“1”中嵌套section level=“2”时,我面临一个问题,并且还希望下面的同级段落应该位于section 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>

介绍
该活动以审查男性的最高程序开始。
女性乳房发育症(切除乳房组织)。
捕捉海浪
我们最初的重点是创建健壮的网站内容。
该活动以审查男性的最高程序开始。
我们的方法:构建平台
我们的标准包括每个服务和登录页至少包含600字
我们最初的重点是创建健壮的网站内容。
捕捉海浪
我们的内容优先战略,以及移动响应设计
我们最初的重点是创建健壮的网站内容。
输出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>

介绍
该活动以审查男性的最高程序开始。
女性乳房发育症(切除乳房组织)。
捕捉海浪
我们最初的重点是创建健壮的网站内容。
该活动以审查男性的最高程序开始。
我们的方法:构建平台
我们的标准包括每个服务和登录页至少包含600字
我们最初的重点是创建健壮的网站内容。
捕捉海浪
我们的内容优先战略,以及移动响应设计
我们最初的重点是创建健壮的网站内容。
和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>

我在输入xml中运行了XSLT,然后我的输出是:

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

介绍
该活动以审查男性的最高程序开始。
女性乳房发育症(切除乳房组织)。
捕捉海浪
我们的方法:构建平台
捕捉海浪
我们最初的重点是创建健壮的网站内容。
该活动以审查男性的最高程序开始。
我们的方法:构建平台
我们的标准包括每个服务和登录页至少包含600字
我们最初的重点是创建健壮的网站内容。
捕捉海浪
我们的内容优先战略,以及移动响应设计
我们最初的重点是创建健壮的网站内容。

考虑对从开始的每个组使用
xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:function name="mf:group" as="node()*">
      <xsl:param name="nodes" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$nodes" group-starting-with="section[@level = $level]">
          <xsl:choose>
              <xsl:when test="self::section[@level = $level]">
                  <xsl:copy>
                      <xsl:apply-templates select="@*, *"/>
                      <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
                  </xsl:copy>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
  </xsl:function>

  <xsl:template match="section[@level = 1 and @level &lt; section/@level]">
      <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:sequence select="mf:group(*, 2)"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

请同时包含您的XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    version="2.0">

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

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:function name="mf:group" as="node()*">
      <xsl:param name="nodes" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$nodes" group-starting-with="section[@level = $level]">
          <xsl:choose>
              <xsl:when test="self::section[@level = $level]">
                  <xsl:copy>
                      <xsl:apply-templates select="@*, *"/>
                      <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
                  </xsl:copy>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
  </xsl:function>

  <xsl:template match="section[@level = 1 and @level &lt; section/@level]">
      <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:sequence select="mf:group(*, 2)"/>
      </xsl:copy>
  </xsl:template> 

</xsl:stylesheet>