Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单例/里程碑元素的XSLT转换_Xslt - Fatal编程技术网

单例/里程碑元素的XSLT转换

单例/里程碑元素的XSLT转换,xslt,Xslt,我正在寻找一个优雅的XSLT1.0或2.0解决方案来解决以下转换问题。我简化了下面的标记,因此“a”是开始里程碑元素,“b”是结束里程碑元素。“a”和“b”之间的所有东西都必须用元素“c”包裹起来 输入: <doc> <line>Text text <a/>text<b/></line> <line>Text <a/>text text</line> <line>Text<b/>

我正在寻找一个优雅的XSLT1.0或2.0解决方案来解决以下转换问题。我简化了下面的标记,因此“a”是开始里程碑元素,“b”是结束里程碑元素。“a”和“b”之间的所有东西都必须用元素“c”包裹起来

输入:

<doc>
<line>Text text <a/>text<b/></line>
<line>Text <a/>text text</line>
<line>Text<b/> text <a/>text</line>
<line>Text text text</line>
<line>Text text<b/> text</line>
</doc>

文本文本文本
文本文本文本
文本文本文本
文本文本文本
文本文本文本
输出:

<doc>
<line>Text text <c>text</c></line>
<line>Text <c>text text</c></line>
<line><c>Text</c> text <c>text</c></line>
<line><c>Text text text</c></line>
<line><c>Text text</c> text</line>
</doc>

文本文本文本
文本文本文本
文本文本文本
文本文本文本
文本文本文本
在现实世界中,每个a/b/c元素至少有5种变化需要满足。还有数万行,其中许多不包含a/b或其变体。现实世界中的案例还有一个“sec”元素,用于包装多组行,其中a/b行为仍然需要处理


我们最初的解决方案涉及使用前面的::*但这显然会给大型XML文档带来严重的性能问题,因此这不是一个可接受的解决方案。

感谢Michaely Kay的启发。暂时忘记了包装纸的方法。。元素,XSLT 3.0中实现的正确算法似乎是:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" />

<xsl:template match="/doc" >
    <xsl:copy>
        <xsl:iterate select="line">
            <xsl:param name="state">b</xsl:param>
            <xsl:variable name="lastState" select="local-name((a|b)[last()])"/>
            <xsl:variable name="nextState">
                <xsl:choose>
                    <xsl:when test="$lastState">
                        <xsl:value-of select="$lastState"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$state"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>

            <xsl:copy>
                <xsl:if test="$state = 'a'"><startC/></xsl:if>
                <xsl:apply-templates/>
                <xsl:if test="$nextState = 'a'"><endC/></xsl:if>                    
            </xsl:copy>

            <xsl:next-iteration>
                <xsl:with-param name="state" select="$nextState"/>
            </xsl:next-iteration>                                        
        </xsl:iterate>
    </xsl:copy>
</xsl:template>

<xsl:template match="a">
    <startC/>
</xsl:template>

<xsl:template match="b">
    <endC/>
</xsl:template>

B

给出输出:

<doc>
    <line>Text text <startC/>text<endC/></line>
    <line>Text <startC/>text text<endC/></line>
    <line><startC/>Text<endC/> text <startC/>text<endC/></line>
    <line><startC/>Text text text<endC/></line>
    <line><startC/>Text text<endC/> text</line>
</doc>

文本文本文本
文本文本文本
文本文本文本
文本文本文本
文本文本文本

只需使用前面的同级而不是前面的同级,就可以解决性能问题。除非这些行包含的节点数量比图中所示的要多,否则前面的同级和后面的同级不应该过于昂贵。谢谢@MichaelKay。我们目前正在使用前面的同级,但它仍然不能扩展,因为我们需要回顾整个文档以正确确定当前的行状态。我猜我们将不得不切换到SAX处理方法,使用一组简单的状态标志来解决这个问题。好吧,我想我还没有理解里程碑标记的语义。这意味着您需要继续前进,通过使用递归或在XSLT 3.0中使用xsl:iterate来维护状态。我没有考虑过xsl:iterate,如果我们可以使用XSLT 3.0转换引擎,这实际上可能对我们有用。谢谢