0
<Request xmlns:ns0="http://Request">
  <Lines>
    <Line>
      <requestid>76</requestid>
      <Code>C001</Code>
    </Line>
    <Line>
      <requestid>77</requestid>
      <Code>C002</Code>
    </Line>
  </Lines>      
  <Conflict>
    <responseid>76</responseid>
    <responsecode>WB</responsecode>
    <cService>
      <responseid>73</responseid>
      <responsecode>HA</responsecode>
    </cService>
    <cService>
      <responseid>7600</responseid>
      <serviceCode>PP</serviceCode>
    </cService>
  </Conflict>      
  <Conflict>
    <responseid>77</responseid>
    <responsecode>WB7</responsecode>
    <cService>
      <responseid>745</responseid>
      <responsecode>HAQ</responsecode>
    </cService>
    <cService>
      <responseid>7234</responseid>
      <serviceCode>PP</serviceCode>
    </cService>
  </Conflict>      
  <Conflict>
    <responseid>77</responseid>
    <responsecode>WBC</responsecode>
    <cService>
      <responseid>72341</responseid>
      <responsecode>HAC</responsecode>
    </cService>
    <cService>
      <responseid>98</responseid>
      <responsecode>PPC</responsecode>
    </cService>
  </Conflict>
</Request>

所需的输出应如下所示。

    <Output xmlns:ns0="http://Response">
      <Lines>
        <Line>
          <responseid>76</responseid>
          <code>WB</code>
          <Features>
            <ExistingFeature>
              <responseid>76</responseid>                           
              <CFeature>
                <responseid>76</responseid>                             
              </CFeature>
              <CFeature>
                <responseid>76</responseid>                                
              </CFeature>
            </ExistingFeature>
          </Features>
        </Line>
        <Line>
          <Num>77</Num>
          <Features>
            <ExistingFeature>
              <responseid>77</responseid>
              <code>WB7</code>
              <CFeature>
                <responseid>77</responseid>                
              </CFeature>
              <CFeature>
                <responseid>77</responseid>                
              </CFeature> 
              <CFeature>
                <responseid>77</responseid>                
              </CFeature> 
          <CFeature>
                <responseid>77</responseid>                
              </CFeature>             
            </ExistingFeature>
          </Features>
        </Line>        
      </Lines>
</Output>

那么您能否帮助我在 XSLT 中实现这一目标。因为我正在尝试多种方式,但由于我在 xslt 方面很差,所以无法做到。

设想。实际上应该为输入中的每一行创建一行,并且基于 requestID(即 76 或 77)的输入中的每一行都有一个或多个冲突。因此,对于输入中每次出现的行以及输入中所有相应的匹配冲突,我必须根据输出中的单个节点中的这些 ID 对冲突进行分组。

所以你能帮我为这个场景写 xslt 吗?

4

2 回答 2

0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="incomekey" match="/Request/Conflict" use="responseid" /> 
  <xsl:template match="/Lines" >
    <Lines>
      <xsl:apply-templates select="Line" />
    </Lines>
  </xsl:template>
  <xsl:template match="Line" >
    <Line>
      <xsl:variable name="tmp" select="requestid" />
      <xsl:for-each select="key('incomekey', $tmp)" >
        <xsl:variable name="strCount" select="position()" />
        <xsl:if test="position() &lt; 2">
          <responseid>
            <xsl:value-of select="requestid/text()" />
          </responseid>
          <code>
            <xsl:value-of select="Code/text()" />
          </code>
        </xsl:if>
        <Features>
          <ExistingFeature>
            <responseid>
              <xsl:value-of select="responseid" />
            </responseid>
            <code>
              <xsl:value-of select="responsecode" />
            </code>
            <xsl:for-each select="cService">
              <CFeature>
                <responseid>
                  <xsl:value-of select="responseid"/>
                </responseid>                              
              </CFeature>
            </xsl:for-each>
          </ExistingFeature>
        </Features>
      </xsl:for-each>
    </Line>
  </xsl:template>
</xsl:stylesheet>

我正在使用上面的脚本。但它为第二行(77)生成输出,如下所示。

于 2012-11-24T20:46:50.520 回答
0
<Line>
        <responseid>77</responseid>
        <code>WB</code>
        <Features>
          <ExistingFeature>
            <responseid>77</responseid>
            <code>WB7</code>
            <CFeature>
              <responseid>745</responseid>
            </CFeature>
            <CFeature>
              <responseid>7234</responseid>
            </CFeature>
          </ExistingFeature>
        </Features>
          <Features>
            <ExistingFeature>
              <responseid>77</responseid>
              <code>WBC</code>
              <CFeature>
                 <responseid>72341</responseid>
              </CFeature>
              <CFeature>
                <responseid>98</responseid>
              </CFeature>
             </ExistingFeature>
        </Features>
      </Line>

如果您观察到此节点,它会为请求中的每个冲突节点创建两个单独的功能节点,其中包含 77(请求 ID)。因此,除此之外,我需要在输出的第二个 Line 节点中填充它,如下所示。

<Features>
    <ExistingFeature>
      <responseid>77</responseid>
      <code>WB7</code>
      <CFeature>
        <responseid>745</responseid>
      </CFeature>
      <CFeature>
        <responseid>7234</responseid>
      </CFeature>
      <CFeature>
        <responseid>72341</responseid>
      </CFeature>
      <CFeature>
        <responseid>98</responseid>
      </CFeature>
    </ExistingFeature>
  </Features>
于 2012-11-24T20:56:11.017 回答