在 xml 层次结构中向上移动分隔符元素

发布于 2024-09-26 14:57:43 字数 767 浏览 0 评论 0原文

我有一个 xml 文档,其分隔符位于层次结构深处。

<A>
  <B>
    <C id='1'/>
    <separator/>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
    <separator/>
  </B>
  <B>
    <C id='4'/>
  </B>
</A>

我想向上移动分隔符,保持元素有序。所以所需的输出是

<A>
  <B>
    <C id='1'/>
  </B>
</A>
<separator/>
<A>
  <B>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
  </B>
</A>
<separator/>
<A>
  <B>
    <C id='4'/>
  </B>
</A>

How can it be done using xslt 1.0 only?可以不使用for-each而仅使用模板匹配来完成吗?

更新: 我实际上得到了 4 个不同程度的通用性的精彩答案,谢谢你们。

I have an xml document with separators deep down in the hierarchy.

<A>
  <B>
    <C id='1'/>
    <separator/>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
    <separator/>
  </B>
  <B>
    <C id='4'/>
  </B>
</A>

I want to move the separators upwards, keeping elements in order. So the desired output is

<A>
  <B>
    <C id='1'/>
  </B>
</A>
<separator/>
<A>
  <B>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
  </B>
</A>
<separator/>
<A>
  <B>
    <C id='4'/>
  </B>
</A>

How can it be done using xslt 1.0 only? Can it be done without for-each, using template match only?

UPDATE:
I actually got 4 brilliant answers of different levels of generality, thank you, guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-10-03 14:57:43

此转换

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

    <xsl:key name="kFollowing" match="C"
         use="generate-id(preceding::separator[1])"/>

 <xsl:template match="/">
  <xsl:apply-templates select="*/*/separator"/>
 </xsl:template>

 <xsl:template match="separator" name="commonSep">
  <separator/>
  <xsl:call-template name="genAncestors">
   <xsl:with-param name="pAncs" select="ancestor::*"/>
   <xsl:with-param name="pLeaves"
        select="key('kFollowing', generate-id())"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template match="separator[not(preceding::separator)]">
  <xsl:call-template name="genAncestors">
   <xsl:with-param name="pAncs" select="ancestor::*"/>
   <xsl:with-param name="pLeaves"
        select="key('kFollowing', '')"/>
  </xsl:call-template>
  <xsl:call-template name="commonSep"/>
 </xsl:template>

 <xsl:template name="genAncestors">
   <xsl:param name="pAncs" select="ancestor::*"/>
   <xsl:param name="pLeaves" select="."/>

   <xsl:choose>
    <xsl:when test="not($pAncs[2])">
     <xsl:apply-templates select="$pLeaves" mode="gen"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:for-each select="$pAncs[1]">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:call-template name="genAncestors">
               <xsl:with-param name="pAncs" select="$pAncs[position()>1]"/>
               <xsl:with-param name="pLeaves" select="$pLeaves"/>
        </xsl:call-template>
      </xsl:copy>
     </xsl:for-each>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

 <xsl:template match="C" mode="gen">
  <xsl:variable name="vCur" select="."/>
  <xsl:for-each select="..">
   <xsl:copy>
    <xsl:copy-of select="@*|$vCur"/>
   </xsl:copy>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<A>
  <B>
    <C id='1'/>
    <separator/>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
    <separator/>
  </B>
  <B>
    <C id='4'/>
  </B>
</A>

产生所需的正确结果

<A>
   <B>
      <C id="1"/>
   </B>
</A>
<separator/>
<A>
   <B>
      <C id="2"/>
   </B>
   <B>
      <C id="3"/>
   </B>
</A>
<separator/>
<A>
   <B>
      <C id="4"/>
   </B>
</A>

This transformation:

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

    <xsl:key name="kFollowing" match="C"
         use="generate-id(preceding::separator[1])"/>

 <xsl:template match="/">
  <xsl:apply-templates select="*/*/separator"/>
 </xsl:template>

 <xsl:template match="separator" name="commonSep">
  <separator/>
  <xsl:call-template name="genAncestors">
   <xsl:with-param name="pAncs" select="ancestor::*"/>
   <xsl:with-param name="pLeaves"
        select="key('kFollowing', generate-id())"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template match="separator[not(preceding::separator)]">
  <xsl:call-template name="genAncestors">
   <xsl:with-param name="pAncs" select="ancestor::*"/>
   <xsl:with-param name="pLeaves"
        select="key('kFollowing', '')"/>
  </xsl:call-template>
  <xsl:call-template name="commonSep"/>
 </xsl:template>

 <xsl:template name="genAncestors">
   <xsl:param name="pAncs" select="ancestor::*"/>
   <xsl:param name="pLeaves" select="."/>

   <xsl:choose>
    <xsl:when test="not($pAncs[2])">
     <xsl:apply-templates select="$pLeaves" mode="gen"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:for-each select="$pAncs[1]">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:call-template name="genAncestors">
               <xsl:with-param name="pAncs" select="$pAncs[position()>1]"/>
               <xsl:with-param name="pLeaves" select="$pLeaves"/>
        </xsl:call-template>
      </xsl:copy>
     </xsl:for-each>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>

 <xsl:template match="C" mode="gen">
  <xsl:variable name="vCur" select="."/>
  <xsl:for-each select="..">
   <xsl:copy>
    <xsl:copy-of select="@*|$vCur"/>
   </xsl:copy>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<A>
  <B>
    <C id='1'/>
    <separator/>
    <C id='2'/>
  </B>
  <B>
    <C id='3'/>
    <separator/>
  </B>
  <B>
    <C id='4'/>
  </B>
</A>

produces the wanted, correct result:

<A>
   <B>
      <C id="1"/>
   </B>
</A>
<separator/>
<A>
   <B>
      <C id="2"/>
   </B>
   <B>
      <C id="3"/>
   </B>
</A>
<separator/>
<A>
   <B>
      <C id="4"/>
   </B>
</A>
一身骄傲 2024-10-03 14:57:43

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kCByFollSep" match="C"
             use="generate-id(following::separator[1])"/>
    <xsl:template match="A">
        <xsl:for-each select="B/separator|B[last()]/*[last()]">
            <A>
                <xsl:apply-templates
                     select="key('kCByFollSep',
                                 substring(generate-id(),
                                           1 div boolean(self::separator)))"/>
            </A>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="C">
        <B>
            <xsl:copy-of select="."/>
        </B>
    </xsl:template>
</xsl:stylesheet>

输出:

<A>
    <B>
        <C id="1" />
    </B>
</A>
<separator />
<A>
    <B>
        <C id="2" />
    </B>
    <B>
        <C id="3" />
    </B>
</A>
<separator />
<A>
    <B>
        <C id="4" />
    </B>
</A>

注意:按分隔符分组,为可能的C添加最后一个第三级元素,而不跟随分隔符 >。

编辑:更多拉样式,更多架构不可知论,此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kCByFollSep" match="C"
             use="generate-id(following::separator[1])"/>
    <xsl:template match="text()"/>
    <xsl:template match="separator|*[not(*)][not(following::*)]">
        <A>
            <xsl:apply-templates
                     select="key('kCByFollSep',
                                 substring(generate-id(),
                                           1 div boolean(self::separator)))"
                     mode="output"/>
        </A>
        <xsl:copy-of select="self::separator"/>
    </xsl:template>
    <xsl:template match="C" mode="output">
        <B>
            <xsl:copy-of select="."/>
        </B>
    </xsl:template>
</xsl:stylesheet>

编辑2:更通用的解决方案(我不信任的一件事,ja!)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pRemains"/>
        <xsl:copy>
            <xsl:apply-templates select="node()[descendant-or-self::node()
                                                   [not(self::separator)]
                                                   [count(following::separator)
                                                    = $pRemains]
                                               ][1]|@*">
                <xsl:with-param name="pRemains" select="$pRemains"/>
            </xsl:apply-templates>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()
                                        [descendant-or-self::node()
                                           [not(self::separator)]
                                           [count(following::separator)
                                            = $pRemains]
                                        ][1]">
            <xsl:with-param name="pRemains" select="$pRemains"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vRemains" select="last()-position()"/>
            <xsl:for-each select="$vCurrent">
                <xsl:copy>
                    <xsl:apply-templates
                         select="node()[descendant::node()
                                          [not(self::separator)]
                                          [count(following::separator)
                                           = $vRemains]
                                       ][1]">
                        <xsl:with-param name="pRemains" select="$vRemains"/>
                    </xsl:apply-templates>
                </xsl:copy>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>

注意:主要是细粒度的遍历。底层层次结构规则(在本例中为根元素)复制自身和分隔符(最后一组没有后续分隔符的虚拟节点)传递余数分隔符以处理第一个子级,并具有足够的后续分隔符来处理。修改后的细粒度遍历身份规则,复制自身并再次处理第一个子级和后续兄弟级,并具有足够的后续分隔符来处理。最后,分隔符规则打破了这个过程。

编辑3:其他更通用的解决方案,现在具有递归身份规则

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
          use="generate-id((descendant::separator|following::separator)[1])"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pGroup"/>
        <xsl:copy>
            <xsl:apply-templates
               select="node()[descendant-or-self::node()[count(.|$pGroup)
                                                         = count($pGroup)]]|@*">
                <xsl:with-param name="pGroup" select="$pGroup"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vGroup"
                 select="key('kNodeByFolSep',generate-id(self::separator))"/>
            <xsl:for-each select="$vCurrent">
                <xsl:call-template name="identity">
                    <xsl:with-param name="pGroup" select="$vGroup"/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>

编辑4:现在与以前相同,但使用键测试而不是节点集交集。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
          use="concat(generate-id(),'+',
                      generate-id((descendant::separator|
                                   following::separator)[1]))"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pSeparator"/>
        <xsl:copy>
            <xsl:apply-templates
               select="@*|node()[descendant-or-self::node()
                                    [key('kNodeByFolSep',
                                         concat(generate-id(),
                                                '+',
                                                $pSeparator))]]">
                <xsl:with-param name="pSeparator" select="$pSeparator"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vSeparator"
                          select="generate-id(self::separator)"/>
            <xsl:for-each select="$vCurrent">
                <xsl:call-template name="identity">
                    <xsl:with-param name="pSeparator" select="$vSeparator"/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kCByFollSep" match="C"
             use="generate-id(following::separator[1])"/>
    <xsl:template match="A">
        <xsl:for-each select="B/separator|B[last()]/*[last()]">
            <A>
                <xsl:apply-templates
                     select="key('kCByFollSep',
                                 substring(generate-id(),
                                           1 div boolean(self::separator)))"/>
            </A>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="C">
        <B>
            <xsl:copy-of select="."/>
        </B>
    </xsl:template>
</xsl:stylesheet>

Output:

<A>
    <B>
        <C id="1" />
    </B>
</A>
<separator />
<A>
    <B>
        <C id="2" />
    </B>
    <B>
        <C id="3" />
    </B>
</A>
<separator />
<A>
    <B>
        <C id="4" />
    </B>
</A>

Note: Grouping by following separator, adding last third level element for posible C without following separator.

Edit: More pull style, more schema agnostic, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kCByFollSep" match="C"
             use="generate-id(following::separator[1])"/>
    <xsl:template match="text()"/>
    <xsl:template match="separator|*[not(*)][not(following::*)]">
        <A>
            <xsl:apply-templates
                     select="key('kCByFollSep',
                                 substring(generate-id(),
                                           1 div boolean(self::separator)))"
                     mode="output"/>
        </A>
        <xsl:copy-of select="self::separator"/>
    </xsl:template>
    <xsl:template match="C" mode="output">
        <B>
            <xsl:copy-of select="."/>
        </B>
    </xsl:template>
</xsl:stylesheet>

EDIT 2: More general solution (one thing I do not trust, ja!)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pRemains"/>
        <xsl:copy>
            <xsl:apply-templates select="node()[descendant-or-self::node()
                                                   [not(self::separator)]
                                                   [count(following::separator)
                                                    = $pRemains]
                                               ][1]|@*">
                <xsl:with-param name="pRemains" select="$pRemains"/>
            </xsl:apply-templates>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()
                                        [descendant-or-self::node()
                                           [not(self::separator)]
                                           [count(following::separator)
                                            = $pRemains]
                                        ][1]">
            <xsl:with-param name="pRemains" select="$pRemains"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vRemains" select="last()-position()"/>
            <xsl:for-each select="$vCurrent">
                <xsl:copy>
                    <xsl:apply-templates
                         select="node()[descendant::node()
                                          [not(self::separator)]
                                          [count(following::separator)
                                           = $vRemains]
                                       ][1]">
                        <xsl:with-param name="pRemains" select="$vRemains"/>
                    </xsl:apply-templates>
                </xsl:copy>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>

Note: Mostly a fine grained traversal. A floor hierarchy rule (in this case root element) copying itself and separator (dummy node for last group without following separator) passing remainder separators to process first child with enough following separators to process. A modified fine grained traversal identity rule, copying itself and again processing first child and following sibling with enough following separators to process. At last, a separator rule breaking the process.

Edit 3: Other more general solution, now with recursive identity rule

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
          use="generate-id((descendant::separator|following::separator)[1])"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pGroup"/>
        <xsl:copy>
            <xsl:apply-templates
               select="node()[descendant-or-self::node()[count(.|$pGroup)
                                                         = count($pGroup)]]|@*">
                <xsl:with-param name="pGroup" select="$pGroup"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vGroup"
                 select="key('kNodeByFolSep',generate-id(self::separator))"/>
            <xsl:for-each select="$vCurrent">
                <xsl:call-template name="identity">
                    <xsl:with-param name="pGroup" select="$vGroup"/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>

Edit 4: Now just the same as before but with key test instead of node set intersection.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:key name="kNodeByFolSep" match="node()[not(self::separator)]"
          use="concat(generate-id(),'+',
                      generate-id((descendant::separator|
                                   following::separator)[1]))"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:param name="pSeparator"/>
        <xsl:copy>
            <xsl:apply-templates
               select="@*|node()[descendant-or-self::node()
                                    [key('kNodeByFolSep',
                                         concat(generate-id(),
                                                '+',
                                                $pSeparator))]]">
                <xsl:with-param name="pSeparator" select="$pSeparator"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*">
        <xsl:variable name="vCurrent" select="."/>
        <xsl:for-each select="descendant::separator|node()[last()]">
            <xsl:variable name="vSeparator"
                          select="generate-id(self::separator)"/>
            <xsl:for-each select="$vCurrent">
                <xsl:call-template name="identity">
                    <xsl:with-param name="pSeparator" select="$vSeparator"/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:copy-of select="self::separator"/>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="separator"/>
</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文