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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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
如何基于根元素中的网格元素创建spanspec元素-XSLT_Xslt_Xslt 2.0 - Fatal编程技术网

如何基于根元素中的网格元素创建spanspec元素-XSLT

如何基于根元素中的网格元素创建spanspec元素-XSLT,xslt,xslt-2.0,Xslt,Xslt 2.0,如何基于根元素中的网格元素基于“网格”创建“spanspec”元素。我已经基于网格元素创建了“colspec”。 输入XML <root> <grid/> <grid/> <grid/> <grid/> </root> 预期产出 <root> <colspec colnum="1" colname="col1"/>

如何基于根元素中的网格元素基于“网格”创建“spanspec”元素。我已经基于网格元素创建了“colspec”。
输入XML

<root>
    <grid/>
    <grid/>
    <grid/>
    <grid/>
</root>

预期产出

 <root>
   <colspec colnum="1" colname="col1"/>
   <colspec colnum="2" colname="col2"/>
   <colspec colnum="3" colname="col3"/>
   <colspec colnum="4" colname="col4"/>
   <spanspec namest="col1" nameend="col2" spanname="1TO2"/>
   <spanspec namest="col1" nameend="col3" spanname="1TO3"/>
   <spanspec namest="col1" nameend="col4" spanname="1TO4"/>
   <spanspec namest="col2" nameend="col3" spanname="2TO3"/>
   <spanspec namest="col2" nameend="col4" spanname="2TO4"/>
   <spanspec namest="col3" nameend="col4" spanname="3TO4"/>
</root>

XSLT代码

    <xsl:template match="root">
    <xsl:variable name="grid-count" select="count(//grid)"/>
    <xsl:copy>
        <xsl:for-each select="grid">
            <colspec colnum="{position()}" colname="{concat('col', position())}"/>
        </xsl:for-each>
        <xsl:for-each select="grid">
            <xsl:variable name="position" select="position()"/>
            <xsl:for-each select="$position to $grid-count - 1">
                <spanspec namest="{concat('col', $position)}" nameend="{concat('col', position()+1)}" spanname="{concat($position,'TO', position()+1)}"/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

我已经解决了

<xsl:template match="root">
    <xsl:variable name="grid-count" select="count(//grid)"/>
    <xsl:copy>
        <xsl:for-each select="grid">
            <colspec colnum="{position()}" colname="{concat('col', position())}"/>
        </xsl:for-each>
        <xsl:for-each select="grid">
            <xsl:variable name="position" select="position()"/>
            <xsl:for-each select="$position to $grid-count - 1">
                <spanspec namest="{concat('col', $position)}" nameend="{concat('col', $position + position())}" spanname="{concat($position,'TO', $position + position())}"/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>