如何修复此XSLT以使其专门为此属性组合分组XML节点?

时间:2012-05-27 23:44:00

标签: xml xslt

输入文件:

<workorders>
    <workorder>
        <renew id="a">
            <nodeA id="N1">
                <fruit id="1" action="aaa">

                    <orange id="x" action="aa">
                        <attributes>
                            <color>Yellow</color>
                            <ada>xxx</ada>
                        </attributes>
                    </orange>

                    <orange id="y" action="change">
                        <attributes>
                            <color>Red</color>
                            <year>2012</year>
                        </attributes>
                    </orange>
                    <orange id="x" action="aa">
                        <attributes>
                            <color>Pink</color>
                            <ada>xxx</ada>
                        </attributes>
                    </orange>
                    <orange id="y" action="change">
                        <attributes>
                            <color>Blue</color>
                            <condition>good</condition>
                        </attributes>
                    </orange>
                </fruit>
            </nodeA>
        </renew>
    </workorder>
</workorders>

我的输出:

<workorders>
    <workorder>
        <renew id="a">
            <nodeA id="N1">
                <fruit id="1" action="aaa">
                    <orange id="x" action="aa">
                        <attributes>
                            <color>Pink</color>
                            <ada>xxx</ada>
                        </attributes>
                    </orange>
                    <orange id="y" action="change">
                        <attributes>
                            <color>Blue</color>
                            <year>2012</year>
                            <condition>good</condition>
                        </attributes>
                    </orange>
                </fruit>
            </nodeA>
        </renew>
    </workorder>
</workorders>

预期输出:

<workorders>
    <workorder>
        <renew id="a">
            <nodeA id="N1">
                <fruit id="1" action="aaa">
                    <orange id="x" action="aa">
                        <attributes>
                            <color>Yellow</color>
                            <ada>xxx</ada>
                        </attributes>
                    </orange>

                    <orange id="x" action="aa">
                        <attributes>
                            <color>Pink</color>
                            <ada>xxx</ada>
                        </attributes>
                    </orange>

                    <orange id="y" action="change">
                        <attributes>
                            <color>Blue</color>
                            <year>2012</year>
                            <condition>good</condition>
                        </attributes>
                    </orange>
                </fruit>
            </nodeA>
        </renew>
    </workorder>
</workorders>

XSL文件:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:a="http://project.com">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="entity" match="/workorders/*/*/*/*/*" use="concat(parent::*/@id, '_', @id, '_', @action)"/>

    <xsl:function name="a:is-primary" as="xs:boolean">
        <xsl:param name="ctx"/>
        <!-- need to establish "focus"(context) for the key() function to work -->
        <xsl:for-each select="$ctx">
            <xsl:sequence select="generate-id($ctx) = generate-id(key('entity', concat($ctx/parent::*/@id, '_', $ctx/@id, '_', $ctx/@action))[1])"/>
        </xsl:for-each>
    </xsl:function> 

    <xsl:function name="a:preceded-by" as="xs:boolean">
        <xsl:param name="ctx"/>
        <xsl:param name="action"/>
        <xsl:value-of select="count($ctx/preceding::*[a:matches($ctx, ., $action)]) > 0"/>
    </xsl:function>

    <xsl:function name="a:followed-by" as="xs:boolean">
        <xsl:param name="ctx"/>
        <xsl:param name="action"/>
        <xsl:value-of select="count($ctx/following::*[a:matches($ctx, ., $action)]) > 0"/>
    </xsl:function>

    <xsl:function name="a:matches" as="xs:boolean">
        <xsl:param name="src"/>
        <xsl:param name="target"/>
        <xsl:param name="action"/>

        <xsl:value-of select="
                     ($src/local-name() = $target/local-name()) and
                      ($src/parent::*/@id = $target/parent::*/@id) and 
                      ($src/@id = $target/@id) and 
                      (if ($action = 'same') 
                          then false()
                          else if ($action = 'any')
                              then ($target/@action = $src/@action)
                              else ($target/@action = $action))"/>  
    </xsl:function>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*/*/*/*/*/*[a:is-primary(.)]" priority="1">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="attributes" mode="consolidate-most-recent"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="attributes" mode="consolidate-most-recent">
        <xsl:copy>
            <xsl:for-each-group 
                        select="/workorders/*/*/*/*/*[a:matches(current()/parent::*, ., 'any')]/attributes/*" 
                        group-by="local-name()">
                <!-- take the last in the document order -->
                <xsl:apply-templates select="current-group()[last()]"/>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/*/*/*/*/*/*[not(a:is-primary(.))]"/>    
   </xsl:stylesheet>

我应该在xsl文件中添加什么才能使转换对于action="change"组合的节点仅 ?现在它将所有其他动作组合起来。

感谢。

1 个答案:

答案 0 :(得分:1)

假设您的预期输出实际上应该保持所有@action != 'change'原样(这意味着每次出现都会打印输出文档中的attributes/*,而不会尝试合并或合并),需要进行两处小改动:

1)将@action = 'change'添加到您的第一个“捕获”模板中:

<xsl:template match="/*/*/*/*/*/*[action = 'change' and a:is-primary(.)]">

2)对“沉默的非主要”模板执行相同的操作:

<xsl:template match="/*/*/*/*/*/*[@action = 'change' and not(a:is-primary(.))]"/>

这将确保所有其他节点都通过身份转换模板。在输入文档上使用这两个修改运行转换会产生:

<workorders>
   <workorder>
      <renew id="a">
         <nodeA id="N1">
            <fruit id="1" action="aaa">
               <orange id="x" action="aa">
                  <attributes>
                     <color>Yellow</color>
                     <ada>xxx</ada>
                  </attributes>
               </orange>
               <orange id="y" action="change">
                  <attributes>
                     <color>Blue</color>
                     <year>2012</year>
                     <condition>good</condition>
                  </attributes>
               </orange>
               <orange id="x" action="aa">
                  <attributes>
                     <color>Pink</color>
                     <ada>xxx</ada>
                  </attributes>
               </orange>
            </fruit>
         </nodeA>
      </renew>
   </workorder>
</workorders>

P.S。如果您希望节点显示按@action排序(如您预期的输出建议),则必须将以下模板添加到组合中:

<xsl:template match="/*/*/*/*/*">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="*">
            <xsl:sort select="@action"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

p.p.s。编写a:matches()函数的方式让我感到困惑。我会在逻辑表达式的最后部分这样做:

(if ($action = 'any') 
    then true()
    else if ($action = 'same')
        then ($target/@action = $src/@action)
        else ($target/@action = $action))

它会显示:如果要求@action,则不关心any属性,如果要求same则比较两者,否则假设提供了某个值比较那个值。然后,您可以使用模板中的same值来调用它:

<xsl:for-each-group 
    select="/workorders/*/*/*/*/*[a:matches(current()/parent::*, ., 'same')]/attributes/*" 
    group-by="local-name()">