0

我对 XSL 有疑问。我有一些必须转换为 xsl 的 xml 文档(类似于 docbook)。一些文件包含sect2-title,下一个兄弟是这个标签的一部分。起始 XML 具有以下结构:

    <?xml version="1.0" encoding="utf-8"?>
    <book>
      <part>
        <article role="content" title="Contenidos">
          <sect1 title="1. Concepto y características de juego">
            <para>El juego es una ...</para>
            <sect2-title>1.1. El modelo lúdico:</sect2-title>
            <para>Sin embargo, sí existe un hecho ...</para>
            <sidebar>
              <sidebar-role>Vocabulario</sidebar-role>
              <sidebar-para>De ahí el origen de ....</sidebar-para>
            </sidebar>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />
              <caption>Fig. 1.2. ....</caption>
            </imageobject>
            <para>En el juego social....</para>
            <sect2-title>1.2. Características ...</sect2-title>
            <para>Dadas ...</para>
            <itemizedlist>
              <listitem>
                <para>• El juego es...</para>
              </listitem>
              <listitem>
                <para>• Es placentera...</para>
              </listitem>
            </itemizedlist>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />
              <caption>Fig. 1.3. ...</caption>
            </imageobject>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />
              <caption>Tabla 1.1. ...</caption>
            </imageobject>
          </sect1>
         </article>
      </part>
    </book>

我想要的结果是:

    <?xml version="1.0" encoding="utf-8"?>
    <book>
      <part>
        <article role="content" title="Contenidos">
          <sect1 title="1. Concepto y características de juego">
            <para>El juego es una ...</para>
            <sect2 title="1.1. El modelo lúdico:">
              <para>Sin embargo, sí existe un hecho ...</para>
              <sidebar>
                <sidebar-role>Vocabulario</sidebar-role>
                <sidebar-para>De ahí el origen de ....</sidebar-para>
              </sidebar>
              <imageobject>
                <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />
                <caption>Fig. 1.2. ....</caption>
              </imageobject>
              <para>En el juego social....</para>
            </sect2>
            <sect2 title="1.2. Características ...">
              <para>Dadas ...</para>
              <itemizedlist>
                <listitem>
                  <para>• El juego es...</para>
                </listitem>
                <listitem>
                  <para>• Es placentera...</para>
                </listitem>
              </itemizedlist>
              <imageobject>
                <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />
                <caption>Fig. 1.3. ...</caption>
              </imageobject>
              <imageobject>
                <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />
                <caption>Tabla 1.1. ...</caption>
              </imageobject>
            </sect2>
          </sect1>
        </article>
      </part>
    </book>

XSL macht 是:

    <xsl:template match="sect1">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each-group select="*[preceding-sibling::sect2-title]"
                                group-starting-with="sect2-title">
                <sect2>
                    <xsl:copy-of select="current-group()"/>
                </sect2>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

有人可以帮助我。我尝试使用 XSL 2.0 但无法获得正确的解决方案。

4

1 回答 1

0

这是一个样式表:

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

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

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

<xsl:template match="sect1">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each-group select="*" group-starting-with="sect2-title">
      <xsl:choose>
        <xsl:when test="self::sect2-title">
          <sect2 title="{.}">
            <xsl:apply-templates select="current-group() except ."/>
          </sect2>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

它转换输入

<?xml version="1.0" encoding="utf-8"?>
    <book>
      <part>
        <article role="content" title="Contenidos">
          <sect1 title="1. Concepto y características de juego">
            <para>El juego es una ...</para>
            <sect2-title>1.1. El modelo lúdico:</sect2-title>
            <para>Sin embargo, sí existe un hecho ...</para>
            <sidebar>
              <sidebar-role>Vocabulario</sidebar-role>
              <sidebar-para>De ahí el origen de ....</sidebar-para>
            </sidebar>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg" />
              <caption>Fig. 1.2. ....</caption>
            </imageobject>
            <para>En el juego social....</para>
            <sect2-title>1.2. Características ...</sect2-title>
            <para>Dadas ...</para>
            <itemizedlist>
              <listitem>
                <para>• El juego es...</para>
              </listitem>
              <listitem>
                <para>• Es placentera...</para>
              </listitem>
            </itemizedlist>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg" />
              <caption>Fig. 1.3. ...</caption>
            </imageobject>
            <imageobject>
              <imagedata fileref="../../media/img/ud01_7151_img01.jpeg" />
              <caption>Tabla 1.1. ...</caption>
            </imageobject>
          </sect1>
         </article>
      </part>
    </book>

进入结果

<?xml version="1.0" encoding="UTF-8"?>
<book>
   <part>
      <article role="content" title="Contenidos">
         <sect1 title="1. Concepto y características de juego">
            <para>El juego es una ...</para>
            <sect2 title="1.1. El modelo lúdico:">
               <para>Sin embargo, sí existe un hecho ...</para>
               <sidebar>
                  <sidebar-role>Vocabulario</sidebar-role>
                  <sidebar-para>De ahí el origen de ....</sidebar-para>
               </sidebar>
               <imageobject>
                  <imagedata fileref="../../media/img/ud01_7151_inl02.jpeg"/>
                  <caption>Fig. 1.2. ....</caption>
               </imageobject>
               <para>En el juego social....</para>
            </sect2>
            <sect2 title="1.2. Características ...">
               <para>Dadas ...</para>
               <itemizedlist>
                  <listitem>
                     <para>• El juego es...</para>
                  </listitem>
                  <listitem>
                     <para>• Es placentera...</para>
                  </listitem>
               </itemizedlist>
               <imageobject>
                  <imagedata fileref="../../media/img/ud01_7151_inl03.jpeg"/>
                  <caption>Fig. 1.3. ...</caption>
               </imageobject>
               <imageobject>
                  <imagedata fileref="../../media/img/ud01_7151_img01.jpeg"/>
                  <caption>Tabla 1.1. ...</caption>
               </imageobject>
            </sect2>
         </sect1>
      </article>
   </part>
</book>
于 2013-05-13T09:24:48.917 回答