【问题标题】:Need to create the empty entry depends on the column需要创建空条目取决于列
【发布时间】:2019-09-12 11:55:16
【问题描述】:

我有三列表格输入 xml,所以 tgroup 将是<tgroup cols="3" colsep="0" rowsep="0">

<row>
   <entry colname="col1" colsep="0" rowsep="0">Duty</entry>
   <entry colname="col2" colsep="0" rowsep="0"></entry>
   <entry colname="col3" colsep="0" rowsep="0">Correct</entry>
</row>
<row>
   <entry colname="col1" colsep="0" rowsep="0">Dollar</entry>
</row>

我试过的XSL是:

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

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

我得到的输出是:

<row>
   <entry>Duty</entry>
   <entry></entry>
   <entry>Correct</entry>
</row>
<row>
   <entry>Dollar</entry>
</row>

异常输出:

<row>
   <entry>Duty</entry>
   <entry></entry>
   <entry>Correct</entry>
</row>
<row>
   <entry>Dollar</entry>
   <entry></entry>
   <entry></entry>
</row>

因此,我遇到了验证错误,这取决于 tgroup 列号。所以我想根据 tgroup 列号在输出中创建空条目。

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

    试试:

    <xsl:template match="row">
        <row>
            <entry>
                <xsl:value-of select="entry[@colname='col1']"/>
            </entry>
            <entry>
                <xsl:value-of select="entry[@colname='col2']"/>
            </entry>
            <entry>
                <xsl:value-of select="entry[@colname='col3']"/>
            </entry>
        </row>
    </xsl:template>
    

    或者只是简单地说:

    <xsl:template match="row">
        <row>
            <entry>
                <xsl:value-of select="entry[1]"/>
            </entry>
            <entry>
                <xsl:value-of select="entry[2]"/>
            </entry>
            <entry>
                <xsl:value-of select="entry[3]"/>
            </entry>
        </row>
    </xsl:template>
    

    甚至:

    <xsl:template match="row">
        <row>
            <xsl:variable name="entries" select="entry" />
            <xsl:for-each select="1 to 3">
                <entry>
                    <xsl:value-of select="$entries[current()]"/>
                </entry>
            </xsl:for-each>
        </row>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      即使您可以在变量中使用 tgroup/@cols 值,并尝试用于生成如下条目:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
      
          <xsl:output indent="yes"/>
      
          <xsl:template match="@* | node()">
              <xsl:copy>
                  <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="row">
              <row>
                  <xsl:variable name="colno" select="../tgroup/@cols"/>
                  <xsl:choose>
                      <xsl:when test="count(entry) = $colno">
                          <xsl:apply-templates/>
                      </xsl:when>
                      <xsl:otherwise>
                          <xsl:apply-templates/>
                          <xsl:for-each select="count(entry) + 1 to $colno">
                              <entry colname="{concat('col',.)}" colsep="0" rowsep="0"/>
                          </xsl:for-each>
                      </xsl:otherwise>
                  </xsl:choose>
              </row>
          </xsl:template>
      
      </xsl:stylesheet>
      

      点击此链接:https://xsltfiddle.liberty-development.net/6rewNxG

      如果您希望 namest 和 nameend 像 spaning 一样,那么您可以使用:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
      
          <xsl:output indent="yes"/>
      
          <xsl:template match="@* | node()">
              <xsl:copy>
                  <xsl:apply-templates select="@* | node()"/>
              </xsl:copy>
          </xsl:template>
      
              <xsl:template match="row">
              <row>
                  <xsl:variable name="colno" select="../tgroup/@cols"/>
                  <xsl:variable name="entrycount" select="count(entry)"/>
                  <xsl:for-each select="entry">
                      <entry>
                          <xsl:apply-templates select="@*"/>
                          <xsl:if test="position() = last() and $colno != $entrycount">
                              <xsl:attribute name="namest" select="concat('col',position())"/>
                              <xsl:attribute name="nameend" select="concat('col',$colno)"/>
                          </xsl:if>
                          <xsl:apply-templates/>
                      </entry>
                  </xsl:for-each>
              </row>
          </xsl:template>
      
      </xsl:stylesheet>
      

      关注链接:https://xsltfiddle.liberty-development.net/6rewNxG/1

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-09
        • 1970-01-01
        • 2022-12-01
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多