0

我对 xslt 很陌生。我正在使用 xslt 1.0。我有一个 xml 文档,它的结构不确定,并且每个部分都不同:

<section label="">
 <subsection label="">
  <para label="">
   <subpara label=""></subpara>
   <subpara label=""></subpara>
  </para>
 </subsection>
</section>
<section label="">
 <subsection label="">

 </subsection>
</section>
<section label="">
 <subsection label="">
   <para label="">

   </para>
 </subsection>
</section>

然后我在循环中遍历每个部分,用每个节点标签的值构建一个字符串,然后使用递归方法复制每个部分节点、带有属性的子节点和文本。

 <xsl:template match="/">
 <xsl:for-each select="//section">
   <xsl:variable name="section">
     <xsl:value-of select="@label"/>
   </xsl:variable>
      <xsl:if test=".//subSection">
        <xsl:for-each select=".//subSection">
          <xsl:variable name="subsection">
            <xsl:if test="string-length(./@label) > '0'">
              (<xsl:value-of select="@label"/>)
            </xsl:if>
          </xsl:variable>

          <xsl:if test=".//para">
            <xsl:for-each select=".//para">
              <xsl:variable name="para">
                <xsl:if test="string-length(./@label) > '0'">
                  (<xsl:value-of select="@label"/>)
                </xsl:if>
              </xsl:variable>

              <xsl:if test=".//subPara">
                <xsl:for-each select=".//subPara">
                  <xsl:variable name="subpara">
                    <xsl:if test="string-length(./@label) > '0'">
                      (<xsl:value-of select="@label"/>)
                    </xsl:if>
                  </xsl:variable>
                  <xsl:if test=".//item">
                    <xsl:for-each select=".//item">
                      <xsl:variable name="item">
                        <xsl:if test="string-length(./@label) > '0'">
                          (<xsl:value-of select="@label"/>)
                        </xsl:if>
                      </xsl:variable>
                      <xmlstringtobuild name="{concat('XML_',$act,' s ',$section,$subsection,$para,$subpara,$item)}"/>
                    </xsl:for-each>
                  </xsl:if>
                  <xmlstringtobuild name="{concat('XML_',$act,' s ',$section,$subsection,$para,$subpara)}"/>
                </xsl:for-each>
              </xsl:if>
              <xmlstringtobuild name="{concat('XML_',$act,' s       ',$section,$subsection,$para)}"/>
            </xsl:for-each>
          </xsl:if>
          <xsl:if test="string-length(@label) > '0'">
            <xmlstringtobuild name="{concat('XML_',$act,' s ',$section,$subsection)}"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:if>
      <xmlstringtobuild name="{concat('XML_',$act,' s ',$section)}"/>
      <xsl:call-template name="LNRecurseToRoot">
        <xsl:with-param name="parent" select="parent::*"/>
        <xsl:with-param name="current">
          <xsl:apply-templates select="." mode="lmt"/>
        </xsl:with-param>
      </xsl:call-template>
 </xsl:for-each>
</xsl:template>

<xsl:template name="LNRecurseToRoot">
<xsl:param name="parent"/>
<xsl:param name="current"/>
<xsl:choose>
  <xsl:when test="not($parent)">
    <xsl:copy-of select="$current"/>
   </xsl:when>
  <xsl:otherwise>
    <xsl:call-template name="LNRecurseToRoot">
      <xsl:with-param name="parent" select="$parent/parent::*"/>
      <xsl:with-param name="current">
        <xsl:for-each select="$parent">
            <xsl:copy>
            <!-- make sure all attributes go across too -->
            <xsl:for-each select="./@*">
              <xsl:copy/>
            </xsl:for-each>
            <!-- add in children along the axis -->
            <xsl:copy-of select="$current"/>
          </xsl:copy>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template> 

我的问题是我想在递归复制输出中的正确位置插入 xmlstringtobuild 元素。(构建该字符串的结束节点,以便可以将特定字符串用作指向特定节、子节、段落等的指针)。您可以将其视为内部链接或锚点。

我可以通过电子邮件向某人发送我当前获得的输出的虚拟数据。您的帮助将不胜感激。谢谢。

4

0 回答 0