0

次の形式のノードを含む着信 XML ドキュメントがあります。

<userOptions>
  <userOption>
      <type>A</type>
      <plan>
          <frequency>MONTHLY</frequency>
      </plan>
      <method>
          <methodType>X1</methodType>
      </method>
  </userOption>

  <userOption>
      <type>B</type>
      <plan>
          <frequency>QUARTERLY</frequency>
      </plan>
      <method>
          <methodType>X2</methodType>
      </method>
  </userOption>

  <userOption>
    <type>A</type>
      <plan>
        <frequency>3MONTH</frequency>
      </plan>
      <method>
        <methodType>X1-B</methodType>
      </method>
  </userOption>
</userOptions>

これを統合して、次のようなものに変換する必要があります。

<userOptions>
    <userOption>
      <type>A</type>
      <plan>
         <frequency>3MONTH</frequency>
         <methodType>X1-B</methodType>
      </plan>
      <plan>
         <frequency>MONTHLY</frequency>
         <methodType>X1</methodType>
      </plan>
    <userOption>
    <userOption>
      <type>B</type>
      <plan>
         <frequency>QUARTERLY</frequency>
         <methodType>X2</methodType>
      </plan>
    <userOption>
</userOptions>

私の現在の変換は次のようになります。

<userOptions>
    <xsl:for-each select="//userOptions">
        <userOption>
            <type>
                <xsl:value-of select="type" />
            </type>

            <xsl:variable name="currentType" select="type"/>
            <xsl:message><xsl:value-of select="$currentType"/></xsl:message>

            <xsl:variable name="currentMethod" select="method/methodType"/>
            <xsl:message><xsl:value-of select="$currentMethod/></xsl:message>

            <plan>
                <frequency>
                    <xsl:value-of select="plan/frequency" />
                </frequency>
                <method>
                    <xsl:value-of select="method/methodType" />
                </method>
            </plan>

            <xsl:for-each select="../userOptions[type=$currentType and method/methodType!=$currentMethod]">

                <plan>
                    <frequency>
                        <xsl:value-of select="plan/frequency" />
                    </frequency>
                    <method>
                        <xsl:value-of select="method/methodType" />
                    </method>
                </plan>

            </xsl:for-each>
        </userOption>
    </xsl:for-each>
</userOptions>

問題は、次のような重複が発生することです。

    <userOption>
      <type>A</type>
      <plan>
         <frequency>3MONTH</frequency>
         <methodType>X1-B</methodType>
      </plan>
      <plan>
         <frequency>MONTHLY</frequency>
         <methodType>X1</methodType>
      </plan>
    <userOption>
    <userOption>
      <type>A</type>
      <plan>
         <frequency>MONTHLY</frequency>
         <methodType>X1</methodType>
      </plan>
      <plan>
         <frequency>3MONTH</frequency>
         <methodType>X1-B</methodType>
      </plan>
    <userOption>

type一致するレコードと異なるレコードを統合する方法がわかりませんmethodTypeが、重複を回避する方法もわかりません。

4

1 に答える 1

1

これはMuenchian グループ化法の良い候補です:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="user-type" match="userOption" use="type" />
  <xsl:template match="/">
    <userOptions>
      <!-- this for-each iterates over a node set consisting of just
      the *first* userOption element for each type -->
      <xsl:for-each select="userOptions/userOption[count( . | key('user-type', type)[1]) = 1]">
        <userOption>
          <xsl:copy-of select="type" />
          <!-- apply templates for all userOption elements of this type
          (including the one we are currently for-eaching over) -->
          <xsl:apply-templates select="key('user-type', type)" />
        </userOption>
      </xsl:for-each>
    </userOptions>
  </xsl:template>

  <xsl:template match="userOption">
    <plan>
      <frequency>
        <xsl:value-of select="plan/frequency" />
      </frequency>
      <method>
        <xsl:value-of select="method/methodType" />
      </method>
    </plan>
  </xsl:template>
</xsl:stylesheet>

これは、ノードのグループを定義し、キーを慎重に使用して、ノードごとではなくグループごとに 1 回反復する気の利いた方法です。XSLT 2 を使用できる場合は、はるかに簡単なメソッドが言語に組み込まれています ( <xsl:for-each-group>) が、XSLT 1 では使用できません。

于 2012-08-13T15:37:51.983 に答える