我需要转换xml文件,并根据xml文件的内容向其中添加一个新节点。例如,我有:
<sheet name="Sheet1" num="1">
<row num="3">
<cell num="1">FP1152</cell>
<cell num="2">1039_2</cell>
<cell num="3">FP000234</cell>
</row>
<row num="4">
<cell num="1">RT1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">GL000235</cell>
</row>
<row num="6">
<cell num="1">FP1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">FP000234</cell>
</row>
</sheet>如果我在不同行的单元格中有相同的值,但在另一对单元格中有不同的值(在我的示例中是带有@num=3和@num=6的行),我想添加一个这样的标志:
<sheet name="Sheet1" num="1">
<flag type="ambiguousSuplier">true<flag>
<row num="3">
<cell num="1">FP1152</cell>
<cell num="2">1039_2</cell>
<cell num="3">FP000234</cell>
</row>
<row num="4">
<cell num="1">RT1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">GL000235</cell>
</row>
<row num="6">
<cell num="1">FP1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">FP000234</cell>
</row>
</sheet>发布于 2012-04-25 20:36:23
如果特定工作表中的行通过@num为1和3的单元格进行分组,那么我可以考虑的一种方法是按键对这些单元格进行分组
<xsl:key
name="cells"
match="cell"
use="concat(../../@num, '|', ../cell[@num='1'], '|', ../cell[@num='3'], '|', @num)" />这将按单元格的页码查找单元格,并在同一行中查找单元格1和2。
然后,您需要检查表是否包含一个单元格,该单元格本身包含上述键中的匹配单元格,但具有不同的值。这是通过以下看起来令人讨厌的语句来实现的
<xsl:template
match="sheet
[row/cell
[text() !=
key('cells',
concat(../../@num, '|', ../cell[@num='1'], '|', ../cell[@num='3'], '|', @num))/text()]]">因此,给定以下XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="cells" match="cell" use="concat(../../@num, '|', ../cell[@num='1'], '|', ../cell[@num='3'], '|', @num)" />
<xsl:template match="sheet[row/cell[text() != key('cells', concat(../../@num, '|', ../cell[@num='1'], '|', ../cell[@num='3'], '|', @num))/text()]]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<flag type="ambiguousSupplier">true</flag>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>当应用于给定的XML时,输出如下
<sheet name="Sheet1" num="1">
<flag type="ambiguousSupplier">true</flag>
<row num="3">
<cell num="1">FP1152</cell>
<cell num="2">1039_2</cell>
<cell num="3">FP000234</cell>
</row>
<row num="4">
<cell num="1">RT1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">GL000235</cell>
</row>
<row num="6">
<cell num="1">FP1152</cell>
<cell num="2">1039_1</cell>
<cell num="3">FP000234</cell>
</row>
</sheet>我相信肯定有一个更简单的解决方案,所以我现在还不会接受这个答案,即使它确实解决了你的问题.
https://stackoverflow.com/questions/10313147
复制相似问题