2012-08-13 22 views
0

不重複我有一個包含表單的一些節點傳入的XML文檔:整合數據和XSLT

<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和不同​​還能避免重複。

回答

1

這是Muenchian grouping方法的良好候選者:

<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> 

這是定義節點的組的一個極好的方法,然後迭代每組一次,而不是每個節點,通過小心使用密鑰。如果您可以使用XSLT 2,則可以使用語言(<xsl:for-each-group>)內置的更直接的方法,但XSLT 1中不提供此方法。

+0

我沒有可用的'xsl:for-each-group',所以我我會試試這個。 – FrustratedWithFormsDesigner 2012-08-13 15:42:49