我正在研究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>
我能够获取值,但我不确定如何根据要求添加属性。
任何人都可以帮我解决问题。
谢谢。
答案 0 :(得分: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>
请注意从强元素
中删除命名空间的额外代码