2

我正在研究 XSLT。

Depending upon the values in the table I have to write the  XML file.

规则:

1.If the values in columns of I row are in strong(Enclosed in<strong> tags).
  Then I have to add attribute as "FirstRowIsStrong".

2.If all the values in the I column are in strong. 
   Then I have to add attribute as "FirstCoulmnIsStrong".

3.If the values in any row are in Strong, 
    then I have to add attribute as  "RowIsStrong". 
    If its the first row I need not add attribute.
    If the values in First row are in strong I should not add attribute.
    For the rows other than First row, I have to add the attribute.

我有这样的源 XML。

        <table style="WIDTH: 100%" border="1" cellspacing="1" cellpadding="1">

        <tr>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">A</strong></td>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">B</strong></td>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">C</strong></td>
        </tr>
        <tr>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">D</strong></td>
        <td>E</td>
        <td>F</td>
        </tr>
        <tr>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">G</strong></td>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">H</strong></td>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">I</strong></td>
        </tr>
        <tr>
        <td><strong xmlns="http://www.w3.org/1999/xhtml">J</strong></td>
        <td>K</td>
        <td>L</td>
        </tr>

        </table>

我应该有输出

<tabledata FirstRowIsStrong="true" FirstCoulmnIsStrong="true">
    <row>
        <column>A</column>
        <column>B</column>
        <column>C</column>          
    </row>
    <row>
        <column>D</column>
        <column>E</column>
        <column>F</column>          
    </row>
    <row RowIsStrong="true">
        <column>G</column>
        <column>H</column>
        <column>I</column>          
    </row>
    <row>
        <column>J</column>
        <column>K</column>
        <column>L</column>          
    </row>
  </tabledata>

我可以获取这些值,但我不确定如何根据要求添加属性。

任何人都可以帮助我如何完成它。

谢谢你。

4

1 回答 1

2

对于FirstRowIsStrong,您需要进行测试以检查第一行不包含没有元素的td元素

<xsl:if test="tr[1][not(td[not(xhtml:strong)])]">

同样,对于FirstColumnIsStrong你需要一个测试来检查没有行首先第一个不包含元素的单元格

<xsl:if test="not(tr[td[1][not(xhtml:strong)]])">

对于RowIsStrong,您可以使用模板匹配来检查非第一行不包含任何此类td元素

<xsl:template match="tr[position() != 1][not(td[not(xhtml:strong)])]">

这是完整的 XSLT

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

   <xsl:template match="table">
      <tabledata>
         <xsl:if test="tr[1][not(td[not(xhtml:strong)])]">
            <xsl:attribute name="FirstRowIsStrong">true</xsl:attribute>
         </xsl:if>
         <xsl:if test="not(tr[td[1][not(xhtml:strong)]])">
            <xsl:attribute name="FirstColumnIsStrong">true</xsl:attribute>
         </xsl:if>
         <xsl:apply-templates select="@*|node()"/>
      </tabledata>
   </xsl:template>

   <xsl:template match="tr[position() != 1][not(td[not(xhtml:strong)])]">
      <row RowIsStrong="true">
         <xsl:apply-templates select="@*|node()"/>
      </row>
   </xsl:template>

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

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

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

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

当应用于您的输入 XML 时,将输出以下内容

<tabledata FirstRowIsStrong="true" FirstColumnIsStrong="true" style="WIDTH: 100%" border="1" cellspacing="1" cellpadding="1">
   <row>
      <column>
         <strong>A</strong>
      </column>
      <column>
         <strong>B</strong>
      </column>
      <column>
         <strong>C</strong>
      </column>
   </row>
   <row>
      <column>
         <strong>D</strong>
      </column>
      <column>E</column>
      <column>F</column>
   </row>
   <row RowIsStrong="true">
      <column>
         <strong>G</strong>
      </column>
      <column>
         <strong>H</strong>
      </column>
      <column>
         <strong>I</strong>
      </column>
   </row>
   <row>
      <column>
         <strong>J</strong>
      </column>
      <column>K</column>
      <column>L</column>
   </row>
</tabledata>

请注意从元素中删除命名空间的额外代码

于 2012-05-28T12:28:04.060 回答