0

如果该组中的所有 tr(item) 在其 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"/>
4

1 回答 1

0

您面临的最大挑战是输入 xml 结构,这是我要解决的唯一挑战(即其余的取决于您)。实际上,您拥有的是一个包含 10 行的表,但您需要将这些行的子集组合在一起,以便您可以隔离属于组 A 的行和属于组 b 的行。很难想出XPath可以做到这一点的 s,但这并非不可能。

例如,此模板将一个tr组的项目隔离在一个变量中,然后复制该变量:

<xsl:template match="a:table/a:tr[@type='group']">
    <xsl:variable name="trs" select="following-sibling::a:tr[ . &lt;&lt; current()/following-sibling::a:tr[@type='total'][1]]" />

    <xsl:copy-of select="$trs" />
</xsl:template>

关键是 XPath following-sibling::a:tr[ . &lt;&lt; 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>
于 2013-10-03T22:33:22.403 回答