分组特定类型的文本节点和相邻元素

3
请建议如何将文本节点和一些像'i'、'b'或'list'这样的元素分组在'p'元素内。确保div不是p元素的子元素。 XML:(为了显示目的而添加换行符或空格,要运行请使用下面的第二个XML)
<article>
<body>
    <para>
        <display><fig>Fig1</fig></display>
        the text node1
    </para>
    <para>
        <display><fig>Fig1</fig></display>
    </para>
    <para>
        <display><fig>Fig1</fig></display>
        the text node1 <i>h</i> ther <b>b</b> the text4
        <display><tab>Table1</tab></display>
        the text node2
        <list><li>list1</li></list>
    </para>
    <para>The text node3</para>

</body>
</article>

XML:(无换行符)

<article><body><para><display><fig>Fig1</fig></display>the text node1</para><para><display><fig>Fig1</fig></display></para><para><display><fig>Fig1</fig></display>the text node1 <i>h</i> ther <b>b</b> the text4<display><tab>Table1</tab></display>the text node2<list><li>list1</li></list></para><para>The text node3</para></body></article>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

<xsl:template match="para">
    <xsl:choose>
        <xsl:when test="not(text())"><xsl:apply-templates/></xsl:when>
        <xsl:when test="display and text() or *">
            <xsl:for-each select="node()">
                <xsl:choose>
                    <xsl:when test="name()='display'"><div><xsl:apply-templates/></div></xsl:when>
                    <xsl:when test="name()='i' or name()='b'">
                        <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
                    </xsl:when>
                    <xsl:when test="not(*)"><p><xsl:value-of select="."/></p></xsl:when><!--Here grouping required with adjacent elements 'i' or 'b' etc -->
                    <xsl:otherwise><p><xsl:apply-templates/></p></xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <p><xsl:apply-templates/></p>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

需要的结果:

<article>
<body>
    <div><fig>Fig1</fig></div><!--ensure div should not child to 'p'-->
    <p>the text node1</p>       <!--Text area including 'i' and 'b' to be within 'p' -->
    <div><fig>Fig1</fig></div>
    <div><fig>Fig1</fig></div>
    <p>the text node1 <i>h</i> ther <b>b</b> the text4</p><!--Text area including 'i' and 'b' to be within 'p' -->
    <div><tab>Table1</tab></div>
    <p>the text node2<list><li>list1</li></list></p><!--text area includes 'list' element -->
    <p>The text node3</p>
</body>
</article>
2个回答

4

由于您使用的是XSLT 2.0,因此您可以在这里使用xsl:for-each-group,根据它们是否为display元素来分组相邻的子节点。

<xsl:for-each-group select="node()" group-adjacent="boolean(self::display)"> 

所以,除了display节点外,其他节点的分组键为false,因此它们将被分组在一起,允许您将它们包裹在一个p标签中。
尝试这个XSLT。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />

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

<xsl:template match="para">
  <xsl:for-each-group select="node()" group-adjacent="boolean(self::display)">
    <xsl:choose>
        <xsl:when test="current-grouping-key()">
           <xsl:apply-templates select="current-group()" />
        </xsl:when>
        <xsl:otherwise>
            <p>
                <xsl:apply-templates select="current-group()" />
            </p>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="display">
   <div>
    <xsl:apply-templates />
   </div>
</xsl:template>
</xsl:stylesheet>

非常感谢您的好建议,点赞。 - Rudramuni TP

1

我认为你可以使用for-each-group group-adjacent="boolean(self::text() | self::i |.."

<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 indent="yes"/>

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

    <xsl:template match="para">
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::text() | self::i | self::b | self::list)">
            <xsl:choose>
                <xsl:when test="current-grouping-key()">
                    <p>
                        <xsl:apply-templates select="current-group()"/>
                    </p>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="display">
        <div>
            <xsl:apply-templates/>
        </div>
    </xsl:template>

</xsl:stylesheet>

感谢您的建议,点赞。 - Rudramuni TP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接