通过 XSLT 根据源 XML 中特定元素的连续出现向 XML 添加属性



我正在研究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>

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

任何人都可以帮助我如何做到这一点。

谢谢。

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

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

类似地,对于第一列是强的,您需要一个测试来检查没有第一行,第一个单元格不包含元素

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

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

相关内容

  • 没有找到相关文章