XSLT模板匹配键值

时间:2015-09-14 04:30:28

标签: xml xslt key template-matching

我有以下输入:

<root>
    <output>         
            <queries>
                <query name="name1">
                    <parameters>
                        <parameter name="id_contact">8947</parameter>
                    </parameters>
                    <queryResults/>
                </query>
                <query name="name1">
                    <parameters>
                        <parameter name="id_contact">8943</parameter>
                    </parameters>
                    <queryResults>
                        <record id="1">
                            <column name="id_task">16422</column>
                            <column name="id_contact">8943</column>
                        </record>
                    </queryResults>
                </query>
                <query name="name1">
                    <parameters>
                        <parameter name="id_contact">1571</parameter>
                    </parameters>
                    <queryResults/>
                </query>
            </queries>
    </output>
    <output2>
        <output_getquerydata>
            <data>
                <query name="name2">
                    <parameters>
                        <parameter name="id">1</parameter>
                    </parameters>
                    <queryResults>
                        <record id="1">
                            <column name="id_task">14016</column>
                            <column name="id_contact">8947</column>
                        </record>
                        <record id="2">
                            <column name="id_task">14012</column>
                            <column name="id_contact">8943</column>
                        </record>
                        <record id="3">
                            <column name="id_task">8826</column>
                            <column name="id_contact">1571</column>
                        </record>
                    </queryResults>
                </query>
                <output_getquerydata>
                    <queries>
                        <query name="name3">
                            <parameters>
                                <parameter name="id_task">14016</parameter>                             
                            </parameters>
                            <queryResults>
                                <record id="1">
                                    <column name="id_shift">2989</column>
                                </record>
                            </queryResults>
                        </query>
                        <query name="name3">
                            <parameters>
                                <parameter name="id_task">14012</parameter>
                            </parameters>
                            <queryResults/>
                        </query>
                        <query name="name3">
                            <parameters>
                                <parameter name="id_task">8826</parameter>                              
                            </parameters>
                            <queryResults/>
                        </query>
                    </queries>
                </output_getquerydata>
            </data>
        </output_getquerydata>
    </output2>
</root>

我的XSL:

<xsl:strip-space elements="*"/>
<xsl:key name="k" match="output/queries/query/queryResults/record" use="column[@name='id_contact']"/>
<xsl:key name="ok" match="output2/output_getquerydata/data/query/queryResults/record" use="column[@name='id_task']"/>    
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<!-- suppress the first output branch -->
<xsl:template match="output"/>
<!-- suppress records that have a matching entry in the other branch -->
<xsl:template match="record[key('k', column[@name='id_contact'])]"/>
<xsl:template match="parameters[not(key('ok', ./parameter))]"/>

目标是每个具有queryResults / record的'query1',我从该queryResults / record / column [@ name ='id_contact']获取值并删除query2中具有该值的每个记录/ queryResults /记录/列[@名= 'id_contact']。这部分有效,但下一部分不能正常工作: 然后,在删除query2的值之后,取剩余的query2 / id_task值并保留id_task中具有相同值的查询[name3]。

期望的输出:

<root>  
    <output2>
        <output_getquerydata>
            <data>
                <query name="name2">
                    <parameters>
                        <parameter name="id">1</parameter>
                    </parameters>
                    <queryResults>
                        <record id="1">
                            <column name="id_task">14016</column>
                            <column name="id_contact">8947</column>
                        </record> 
                        <!--record no.22 deleted, because id_contact=8943 is a match in query1-->
                        <record id="3">
                            <column name="id_task">8826</column>
                            <column name="id_contact">1571</column>
                        </record>
                    </queryResults>
                </query>
                <output_getquerydata>
                    <queries>
                        <query name="name3">
                            <parameters>
                                <parameter name="id_task">14016</parameter>                             
                            </parameters>
                            <queryResults>
                                <record id="1">
                                    <column name="id_shift">2989</column>
                                </record>
                            </queryResults>
                        </query>
                        <!--2nd query name3 deleted, because id_task=14012 is not a match in remaining query2 values-->

                        <query name="name3">
                            <parameters>
                                <parameter name="id_task">8826</parameter>                              
                            </parameters>
                            <queryResults/>
                        </query>
                    </queries>
                </output_getquerydata>
            </data>
        </output_getquerydata>
    </output2>
</root>

我做错了什么? 谢谢

1 个答案:

答案 0 :(得分:0)

不起作用的模板匹配就是这个......

<xsl:template match="parameters[not(key('ok', ./parameter))]"/>

首先,您应该真正匹配query节点,因为这是您要删除的节点。

其次,模板与输入XML匹配,而不是输出XML。您可能正在从输出中删除记录,但输入未更改,因此这将匹配。

因此,您需要做的是检查query2值是否存在,如果它存在,则检查是否将删除相同的值(即它存在于{{1}中}

将上述模板匹配替换为此模板,并查看是否有所不同:

query1