之前和之后-处理说明-XSLT 2

时间:2018-07-11 14:18:30

标签: xslt xslt-2.0

  1. 我正在尝试匹配所有具有{{1}和<row>的同级元素的<?changetrack remove_begin?>元素
  2. 如果找到匹配项,则搜索<?changetrack remove_end?>属性并添加等效的空行。
  3. 请参阅我的XSLT,我需要在max(entry@morerows)中添加此逻辑(虚拟行创建),因此无需修改@format的现有代码。

输入XML:

<xsl:template match="row">

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
    <table format="wide">
       <tgroup>
          <thead>
             <row>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
                <entry>lorem ipsumrow entry</entry>
             </row>
          </thead>
          <tbody>
             <?changetrack remove_begin?>
             <row>
                <entry>first row entryrow entry</entry>
                <entry>firstrow entry</entry>
                <entry>firstrow entry</entry>
                <entry morerows="3">first row entry</entry>
                <entry>firstrow entry</entry>
                <entry>firstrow entry</entry>
                <entry>firstrow entry</entry>
                <entry>firstrow entry</entry>
             </row>
             <?changetrack remove_end?>
             <?changetrack insert_begin?>
             <row>
                <entry>secondrow entry</entry>
                <entry>secondrow entry</entry>
                <entry>secondrow entry</entry>
                <entry morerows="2">secondrow entry</entry>
                <entry>secondrow entry</entry>
                <entry>secondrow entry</entry>
                <entry>secondrow entry</entry>
                <entry>secondrow entry</entry>
             </row>
             <?changetrack insert_end?>
             <row>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
                <entry>Thirdrow entry</entry>
             </row>
             <?changetrack remove_begin?>
             <row>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
                <entry>Forthrow entry</entry>
             </row>
             <?changetrack remove_end?>            
             <row>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
                <entry>fifthrow entry</entry>
             </row>            
             <row>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
                <entry>sixthrow entry</entry>
             </row>
          </tbody>
       </tgroup>
    </table>

我的XSLT:与PI不匹配

 <?xml version="1.0" encoding="UTF-8"?>
<table format="wide">
    <tgroup>
        <thead>
            <forow>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
                <entry format="wide">lorem ipsumforow entry</entry>
            </forow>
        </thead>
        <tbody>
            <?changetrack remove_begin?>
            <forow>
                <entry format="wide">first forow entryforow entry</entry>
                <entry format="wide">firstforow entry</entry>
                <entry format="wide">firstforow entry</entry>
                <entry moreforows="3">first forow entry</entry>
                <entry format="wide">firstforow entry</entry>
                <entry format="wide">firstforow entry</entry>
                <entry format="wide">firstforow entry</entry>
                <entry format="wide">firstforow entry</entry>
            </forow>
            <forow>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
            </forow>
            <forow>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
            </forow>
            <forow>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
                <entry format="wide">Dummy forow entry</entry>
            </forow>
            <?changetrack remove_end?>
            <?changetrack insert_begin?>
            <forow>
                <entry format="wide">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
                <entry moreforows="2">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
                <entry format="wide">secondforow entry</entry>
            </forow>
            <?changetrack insert_end?>
            <forow>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
                <entry format="wide">Thirdforow entry</entry>
            </forow>
            <?changetrack remove_begin?>
            <forow>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
                <entry format="wide">Forthforow entry</entry>
            </forow>
            <?changetrack remove_end?>
            <forow>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
                <entry format="wide">fifthforow entry</entry>
            </forow>
            <forow>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
                <entry format="wide">sixthforow entry</entry>
            </forow>
        </tbody>
    </tgroup>
</table>

1 个答案:

答案 0 :(得分:2)

您的XSLT中有几个问题。首先,这个表达式...

preceding-sibling::processing-instruction('changetrack')[contains(., 'remove_begin')][1]

此表达式正在获取包含“ remove_begin”的第一个在前的“ changetrack”处理,但可能不是紧接在前的同级。可以追溯到XML

您想要的表达式是这个(后继同级类似)

 preceding-sibling::node()[normalize-space()][1][self::processing-instruction('changetrack')][contains(., 'remove_begin')]

(您可以删除normalize-space()检查XSLT是否执行了xsl:strip-space

第二,<xsl:for-each select="$morerows">仅要迭代一次,因为$morerows仅包含一个原子类型(此值不被使用,仅存在其中之一)。

您需要做的是...

<xsl:for-each select="1 to xs:int($morerows)">

xs被声明为“ http://www.w3.org/2001/XMLSchema”的地方

尝试使用此XSLT

<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                exclude-result-prefixes="xs">
    <xsl:output method="xml" indent="yes" />

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

    <xsl:template match="row">
        <xsl:variable name="format" select="ancestor::table/@format"/>
        <xsl:choose>
            <xsl:when test="$format = 'wide'">
                <forow format="wide">
                    <xsl:apply-templates/>
                </forow>
            </xsl:when>
            <xsl:when test="$format = 'compact'">
                <forow format="compact">
                    <xsl:apply-templates/>
                </forow>
            </xsl:when>
            <xsl:otherwise>
                <forow>
                    <xsl:apply-templates/>
                </forow>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:if
            test="preceding-sibling::node()[normalize-space()][1][self::processing-instruction('changetrack')][contains(., 'remove_begin')] and following-sibling::node()[normalize-space()][1][self::processing-instruction('changetrack')][contains(., 'remove_end')]">
            <xsl:variable name="morerows" select="max(entry/@morerows)"/>
            <xsl:for-each select="1 to xs:int($morerows)">
                <row>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                    <entry>Dummay row entry</entry>
                </row>
            </xsl:for-each>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>