Unwarp tags 并在 expect single Tag 之间放置逗号

Unwarp tags and put comma in between expect single Tag

根据我们的要求,我们需要将由 comma.We 分隔的 para 的内容包装起来。 但是不应该考虑逗号的 thiru 标签,它必须附加到上一个标签或下一个标签。 如果只有 para 是 before 和 after 那么它应该附加两者。

见下例:

输入4:

<Para>Apple1
    <Thiru>Mango1<Ref>Grape1</Ref><Ref>Grape2</Ref><Ref>Grape3</Ref>Mango2</Thiru>Apple2
</Para>

输出4:

<Para>Apple1Mango1,Grape1,Grape2,Grape3Mango2,Apple2</Para>

当前 xsl:

<xsl:copy-of select="$Cells/Para/@*" />
<xsl:for-each select="$Cells/Para/node()[self::text() or self::Ref or self::Thiru][normalize-space(.)!='']">
<xsl:value-of select="normalize-space(.)" />
<xsl:if test="position()!=last()" >
<xsl:value-of select="','" />
</xsl:if>
</xsl:for-each>

通过使用我们得到的当前 xsl,对于所有 element.My 要求,我们不应该考虑 Tag Thiru。

请验证示例输入和输出。

查看您当前的输入和预期输出,我假设以下规则

  1. 不要在第一个文本元素前放置逗号
  2. 不要在父元素为 Thiru
  3. 的文本元素前放置逗号

在这种情况下,试试这个样式表:(注意我已经颠倒了逻辑,所以它实际上是检查逗号是否应该放置,而不是不放置)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Para">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select=".//text()">
                <xsl:if test="position() > 1 and not(parent::Thiru)">,</xsl:if>
                <xsl:value-of select="normalize-space()" />
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

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

当这应用于以下输入时:

<Para>Apple1
    <Thiru>Mango1<Ref>Grape1</Ref><Ref>Grape2</Ref><Ref>Grape3</Ref>Mango2</Thiru>Apple2
</Para>

下面是输出

<Para>Apple1Mango1,Grape1,Grape2,Grape3Mango2,Apple2</Para>