我想,如果该组中的所有tr(项目)在其td(index = 1)元素的值中具有该后缀,则移动(从项目中删除并追加到组)后缀。这意味着'[s2]'应移至A组,[s1]移至B组
此XML应为:
<table xmlns="MyNamespaceUri">
<tr type="group"> <td index="1">Group A</td> </tr>
<tr type="item">
<td index="1"> Item Type A1 [s1],[s2],[s3]</td>
<td index="2">11</td>
</tr>
<tr type="item">
<td index="1"> Item Type A2 [s1],[s2]</td>
<td index="2">21</td>
</tr>
<tr type="item">
<td index="1"> Item Type A3 [s2],[s4]</td>
<td index="2">31</td>
</tr>
<tr type="total"> <td index="2">63</td> </tr>
<tr type="group">
<td index="1">Group B</td>
</tr>
<tr type="item">
<td index="1"> Item Type B1 [s1],[s2],[s3]</td>
<td index="2">12</td>
</tr>
<tr type="item">
<td index="1"> Item Type B2 [s1],[s3]</td>
<td index="2">22</td>
</tr>
<tr type="item">
<td index="1"> Item Type B3 [s1],[s4]</td>
<td index="2">32</td>
</tr>
<tr type="total"> <td index="2">66</td> </tr>
</table>
像这样转变:
<table xmlns="MyNamespaceUri">
<tr type="group"> <td index="1">Group A [s2]</td> </tr>
<tr type="item">
<td index="1"> Item Type A1 [s1],[s3]</td>
<td index="2">11</td>
</tr>
<tr type="item">
<td index="1"> Item Type A2 [s1]</td>
<td index="2">21</td>
</tr>
<tr type="item">
<td index="1"> Item Type A3 [s4]</td>
<td index="2">31</td>
</tr>
<tr type="total"> <td index="2">63</td> </tr>
<tr type="group">
<td index="1">Group B [s1]</td>
</tr>
<tr type="item">
<td index="1"> Item Type B1 [s2],[s3]</td>
<td index="2">12</td>
</tr>
<tr type="item">
<td index="1"> Item Type B2 [s3]</td>
<td index="2">22</td>
</tr>
<tr type="item">
<td index="1"> Item Type B3 [s4]</td>
<td index="2">32</td>
</tr>
<tr type="total"> <td index="2">66</td> </tr>
</table>
提前感谢。
编辑:
作为XSL / T的新手,这是我在工作了几天之后所拥有的。我无法让“替换”功能起作用;抛出了一个未找到的函数或有效的异常(虽然我改为版本2.0);然后尝试翻译(这不适合这个问题)。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="MyNamespaceUri" version="2.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @* "/>
</xsl:copy>
</xsl:template>
<xsl:variable name="arg1" select="'[s2]'"></xsl:variable>
<xsl:template match="a:table/a:tr[@type='group']/a:td">
<xsl:copy-of select="."/>
<xsl:variable name="arg2" select="../../a:tr[@type='item']/a:td[@index='1']"></xsl:variable>
<td>
<xsl:if test="contains($arg2,$arg1)='true'">
<xsl:value-of select="$arg1" />
</xsl:if>
</td>
</xsl:template>
<xsl:template match="a:tr[@type='item']/a:td[@index='1']">
<td index="1">
<xsl:value-of select="translate(., $arg1, '')" />
</td>
</xsl:template>
<xsl:template match="a:table/a:tr[@type='group']/a:td"/>
答案 0 :(得分:0)
你面临的最大挑战是输入xml结构,这是我要解决的唯一挑战(其余的由你决定)。您真正拥有的是一个包含10行的表,但您需要将这些行的子集组合在一起,这样您就可以将属于的行隔离到A组和所属的行到b组。很难想出能够做到这一点的XPath
,但这并非不可能。
例如,此模板在变量中隔离tr
组的项目,然后复制变量:
<xsl:template match="a:table/a:tr[@type='group']">
<xsl:variable name="trs" select="following-sibling::a:tr[ . << current()/following-sibling::a:tr[@type='total'][1]]" />
<xsl:copy-of select="$trs" />
</xsl:template>
密钥是XPath following-sibling::a:tr[ . << current()/following-sibling::a:tr[@type='total'][1]]
- 这意味着“选择tr
以后的所有兄弟节点,直到第一次出现tr
为type='total'
}}
由于您现在能够将item
行隔离到它们自己的变量中,您现在可以继续编写代码来测试变量的td/text()
值的内容是否存在匹配String
并根据需要添加条件逻辑。
您的代码也存在一些命名空间问题。在复制时,请确保在此过程中不会丢失输入的命名空间 - 注意copy-namespaces="yes"
:
<xsl:template match="node() | @*">
<xsl:copy copy-namespaces="yes">
<xsl:apply-templates select="node() | @* "/>
</xsl:copy>
</xsl:template>