【问题标题】:Need to create the template by using the count of row entry需要使用行条目数创建模板
【发布时间】:2019-09-18 10:12:31
【问题描述】:

我的输入 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 个条目的情况。我想通过使用行内的条目数来创建新的条目模板。请提出建议。

【问题讨论】:

  • xsl:attribute 的用例是动态属性的名称。改用AVT&lt;entry colname="{@colname}" colsep="{@colsep}" rowsep="{@rowsep}"&gt;&lt;xsl:apply-templates/&gt;&lt;/entry&gt;

标签: xml xslt xslt-2.0


【解决方案1】:

也许你可以有一个变量来存储entry 元素的最大数量在row...

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

然后,有一个模板匹配row 没有最大entry 数量的元素,并使用xsl:for-each 追加新的空元素

<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>

试试这个 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>

【讨论】:

  • 我在输入中也有这种类型:&lt;entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3"&gt;Temp&lt;/entry&gt; 所以它正在为这种类型创建空条目。
  • 如果您确实想要与所要求的不同的东西,您真的应该修改您的问题。谢谢!
  • 我在为&lt;xsl:variable name="start" select="xs:int(substring-after(//entry/@namest, 'col'))" /&gt; 转换A sequence of more than one item is not allowed as the first argument of substring-after() ("col1", "col1", ...) 时遇到此错误
  • 您是否拥有多个带有namest 属性的entry 元素?如果是这样,您可以编辑您的 XML 以显示这种情况吗?谢谢!
猜你喜欢
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
相关资源
最近更新 更多