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
Xml 需要使用行条目的计数来创建模板_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Xml 需要使用行条目的计数来创建模板

Xml 需要使用行条目的计数来创建模板,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我的输入xml如下所示: <row> <entry colname="col1" colsep="0" rowsep="0">Get</entry> <entry colname="col2" colsep="0" rowsep="0">Some</entry> <entry colname="col3" colsep="0" rowsep="0">Manual</entry> </row

我的输入xml如下所示:

<row>
   <entry colname="col1" colsep="0" rowsep="0">Get</entry>
   <entry colname="col2" colsep="0" rowsep="0">Some</entry>
   <entry colname="col3" colsep="0" rowsep="0">Manual</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0">Skip</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Temp</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Task</entry>
</row>
我使用的XSL如下所示:

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

<xsl:template match="entry">
    <entry>
        <xsl:attribute name="colname">
            <xsl:value-of select="@colname"/>
        </xsl:attribute>
        <xsl:attribute name="colsep">
            <xsl:value-of select="@colsep"/>
        </xsl:attribute>
        <xsl:attribute name="rowsep">
            <xsl:value-of select="@rowsep"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </entry>
</xsl:template>

<xsl:template match="entry[@colname and @namest and @nameend]">
        <entry>
            <xsl:attribute name="colname">
                <xsl:value-of select="@colname"/>
            </xsl:attribute>
            <xsl:attribute name="colsep">
                <xsl:value-of select="@colsep"/>
            </xsl:attribute>
            <xsl:attribute name="rowsep">
                <xsl:value-of select="@rowsep"/>
            </xsl:attribute>
            <xsl:attribute name="namest">
                <xsl:value-of select="@namest"/>
            </xsl:attribute>
            <xsl:attribute name="nameend">
                <xsl:value-of select="@nameend"/>
            </xsl:attribute>
            <xsl:apply-templates/>
        </entry>
    </xsl:template>
预期产出如下:

<row>
   <entry colname="col1" colsep="0" rowsep="0">Get</entry>
   <entry colname="col2" colsep="0" rowsep="0">Some</entry>
   <entry colname="col3" colsep="0" rowsep="0">Manual</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0">Skip</entry>
   <entry colname="col2" colsep="0" rowsep="0"/>
   <entry colname="col3" colsep="0" rowsep="0">
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Temp</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Task</entry>
</row>

有很多条目具有namest和nameend属性。对于行中有3个条目的情况,上面的条目模板运行良好。我想通过使用行中的条目计数来创建条目的新模板。请建议。

也许您可以使用一个变量来存储一行中最大数量的条目元素

尝试以下XSLT:

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

  <xsl:output method="xml" indent="yes" />

  <xsl:variable name="cols" select="max(//row[not(entry/@namest)]/count(entry))" />

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

  <xsl:template match="row[not(entry[$cols]) and not(entry/@namest)]">
    <xsl:copy>
      <xsl:apply-templates />
      <xsl:for-each select="count(entry) + 1 to $cols">
        <entry colname="col{.}" colsep="0" rowsep="0"></entry>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

我在input:Temp中也有这个类型,所以它为这个类型创建了一个空条目。如果你真的想要一些不同于所问内容的东西,你真的应该修改你的问题。谢谢当转换一个包含多个项的序列不允许作为col1,col1。。。对于,是否有多个entry元素具有namest属性?如果是这样的话,您可以编辑XML以显示这种情况吗?谢谢attribute的用例是动态属性的名称。改用:
<xsl:template match="row[not(entry[$cols])]">
  <xsl:copy>
    <xsl:apply-templates />
    <xsl:for-each select="count(entry) + 1 to $cols">
      <entry colname="col{.}" colsep="0" rowsep="0"></entry>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:variable name="cols" select="max(//row[not(entry/@namest)]/count(entry))" />

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

  <xsl:template match="row[not(entry[$cols]) and not(entry/@namest)]">
    <xsl:copy>
      <xsl:apply-templates />
      <xsl:for-each select="count(entry) + 1 to $cols">
        <entry colname="col{.}" colsep="0" rowsep="0"></entry>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>