2012-05-28 1 views
2

Я работаю над 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> 

Я мог бы в состоянии получить значение, но я не уверен, как добавить атрибуты в соответствии с требованием.

Может ли кто-нибудь помочь мне, как это можно сделать.

спасибо.

ответ

2

Для FirstRowIsStrong вам нужен тест, чтобы проверить там первая строка не содержит TD элемента, который не имеет сильных элемента

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

Similary, для 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> 

Обратите внимание на дополнительный код для удаления пространства имен с прочный элемент

+0

+1 для хорошего ответа. –

+0

Спасибо за отличный ответ. – Patan