Xslt 基于特定文本拆分XML节点

Xslt 基于特定文本拆分XML节点,xslt,Xslt,请建议如何根据特定注释文本()拆分xml节点。我尝试使用xsl:text-disable-output转义格式来放置所需的结束和开始标记(元素) 我的代码是静态的,如何使动态代码适用于任何数量的祖先注释文本,即,如果祖先计数“n”,那么从n到1如何使用调用模板方法 一些空节点不是内容的祖先,如何避免这些 我参考了我们网站上的一些建议,但发现很难理解其中的逻辑。请建议。(XSLT2版本) XSLT: 文本1 文本2文本3文本4 文本5 文本6 XSLT: / / / / / / / /

请建议如何根据特定注释文本()拆分xml节点。我尝试使用xsl:text-disable-output转义格式来放置所需的结束和开始标记(元素)

  • 我的代码是静态的,如何使动态代码适用于任何数量的祖先注释文本,即,如果祖先计数“n”,那么从n到1如何使用调用模板方法
  • 一些空节点不是内容的祖先,如何避免这些
我参考了我们网站上的一些建议,但发现很难理解其中的逻辑。请建议。(XSLT2版本)

XSLT:


文本1
文本2文本3文本4
文本5
文本6
XSLT:


/
/
/
/
/
/
/
/
/
期望输出:

<root>
    <a>
        <b>The text1
            <c>
                <d>The text2</d>
                <d>The text3</d>
            </c>
        </b>
    </a>
</root>
<!--Break-->
<root>
    <a>
        <b>
            <c>
                <d>The text4</d>
                <e>The text5</e>
            </c>
        </b>
    </a>
</root>
<!--Break-->
<root>
    <a>
        <b>
            <f>The text6</f>
        </b>
    </a>
</root>

文本1
文本2
文本3
文本4
文本5
文本6
[groan]你在哪里找到这些“有趣”的任务

不应该要求您使用转义文本手动形成XML节点。 尝试以下样式表。它可能更加精简,但我相信它解决了这里的主要问题:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="root" select="/*" />

<xsl:template match="/">
    <super-root>
        <xsl:for-each select="0 to count(//comment()[.='Break'])">
            <xsl:apply-templates select="$root">
                <xsl:with-param name="i" select="." tunnel="yes"/>
            </xsl:apply-templates>
            <xsl:if test="position()!=last()">
                <xsl:comment>Break</xsl:comment>
            </xsl:if>
        </xsl:for-each >
    </super-root>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:param name="i" tunnel="yes"/>
        <xsl:if test="descendant-or-self::text()[count(preceding::comment()[.='Break'])=$i]">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

打破
结果

<?xml version="1.0" encoding="UTF-8"?>
<super-root>
   <root>
      <a>
         <b>The text1
            <c>
               <d>The text2</d>
               <d>The text3</d>
            </c>
         </b>
      </a>
   </root>
   <!--Break-->
   <root>
      <a>
         <b>
            <c>
               <d>The text4</d>
               <e>The text5</e>
            </c>
         </b>
      </a>
   </root>
   <!--Break-->
   <root>
      <a>
         <b>
            <f>The text6</f>
         </b>
      </a>
   </root>
</super-root>

文本1
文本2
文本3
文本4
文本5
文本6
[groan]你在哪里找到这些“有趣”的任务

不应该要求您使用转义文本手动形成XML节点。 尝试以下样式表。它可能更加精简,但我相信它解决了这里的主要问题:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="root" select="/*" />

<xsl:template match="/">
    <super-root>
        <xsl:for-each select="0 to count(//comment()[.='Break'])">
            <xsl:apply-templates select="$root">
                <xsl:with-param name="i" select="." tunnel="yes"/>
            </xsl:apply-templates>
            <xsl:if test="position()!=last()">
                <xsl:comment>Break</xsl:comment>
            </xsl:if>
        </xsl:for-each >
    </super-root>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:param name="i" tunnel="yes"/>
        <xsl:if test="descendant-or-self::text()[count(preceding::comment()[.='Break'])=$i]">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

打破
结果

<?xml version="1.0" encoding="UTF-8"?>
<super-root>
   <root>
      <a>
         <b>The text1
            <c>
               <d>The text2</d>
               <d>The text3</d>
            </c>
         </b>
      </a>
   </root>
   <!--Break-->
   <root>
      <a>
         <b>
            <c>
               <d>The text4</d>
               <e>The text5</e>
            </c>
         </b>
      </a>
   </root>
   <!--Break-->
   <root>
      <a>
         <b>
            <f>The text6</f>
         </b>
      </a>
   </root>
</super-root>

文本1
文本2
文本3
文本4
文本5
文本6

感谢您将OP从禁用输出转义中重定向出去。@Michael,非常感谢您编写的漂亮XSLT代码。它正按要求完美地工作。如果有任何进一步的要求,我可以要求相关的这个职位吗?请。@Michael,如果元素“b”中需要拆分,请提出建议。@RudramuniTP我相信您可以选择任何节点作为“断开节点”,而不是
@Michael中的注释,很抱歉我的请求不明确。不再多次使用根节点,现在“b”是break的最顶层祖先(“b”将多次出现),并且主根也将出现一次。Break将取决于通常的注释“Break”。为将OP重定向到禁用输出转义而鼓掌。@Michael,非常感谢您编写的漂亮XSLT代码。它正按要求完美地工作。如果有任何进一步的要求,我可以要求相关的这个职位吗?请。@Michael,如果元素“b”中需要拆分,请提出建议。@RudramuniTP我相信您可以选择任何节点作为“断开节点”,而不是
@Michael中的注释,很抱歉我的请求不明确。不再多次使用根节点,现在“b”是break的最顶层祖先(“b”将多次出现),并且主根也将出现一次。中断将取决于通常的“中断”注释。