使用xslt基于来自不同xml的子节点比较两个节点

时间:2014-06-17 15:54:47

标签: xml xslt

我有两个XML: -

  1. keylist.xml

    <?xml version="1.0" encoding="UTF-8"?><jdbcGeneralActivityOutput>
    <unknownResultset>
        <row>
            <column>
                <name>SRC_SYS_ID</name>
                <value>10015</value>
            </column>
            <column>
                <name>SRC_SYS_RCRD_ID_VAL</name>
                <value>3327853</value>
            </column>
        </row>
        <row>
            <column>
                <name>SRC_SYS_ID</name>
                <value>10015</value>
            </column>
            <column>
                <name>SRC_SYS_RCRD_ID_VAL</name>
                <value>9751818</value>
            </column>
        </row>
        <row>
            <column>
                <name>SRC_SYS_ID</name>
                <value>10015</value>
            </column>
            <column>
                <name>SRC_SYS_RCRD_ID_VAL</name>
                <value>9883123</value>
            </column>
        </row>
        <row>
            <column>
                <name>SRC_SYS_ID</name>
                <value>10015</value>
            </column>
            <column>
                <name>SRC_SYS_RCRD_ID_VAL</name>
                <value>9901061</value>
            </column>
        </row>
    </unknownResultset>    </jdbcGeneralActivityOutput>
    
  2. 和第二个XML 2. CurrentRow.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <row>
        <column>
            <name>SRC_SYS_ID</name>
            <value>10015</value>
        </column>
        <column>
            <name>SRC_SYS_RCRD_ID_VAL</name>
            <value>3327853</value>
        </column>
        <column>
            <name>PRTY_STTS_CDV</name>
            <value>A</value>
        </column>
        <column>
            <name>PRTY_DSPLY_NM</name>
            <value>ALEGRIA BRAZILIAN GRILL</value>
        </column>
        <column>
            <name>ADDR_LN_1_TXT</name>
            <value>24449 KATY FRWY SUITE 500</value>
        </column>
        <column>
            <name>ADDR_LN_2_TXT</name>
            <value/>
        </column>
        <column>
            <name>ADDR_LN_3_TXT</name>
            <value/>
        </column>
    </row>
    

    我想要做的是找出keylist.xml中是否有完整行(所有行中的所有名称值对应)都存在于currentRow.xml中。

    这是我到目前为止所尝试的...但没有运气。任何人都可以帮助我

        <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:param name="currentRow" select="document('C:\Personal\09086559\Desktop\Xmls\currentRow.xml')"/>
        <!--<xsl:param name="TargetPrimaryKeys" select="document('C:\Personal\09086559\Desktop\Xmls\temp5.xml')"/>
        -->
        <xsl:template match="/">
            <xsl:variable name="apos">'</xsl:variable>
            <xsl:for-each select="jdbcGeneralActivityOutput/unknownResultset/row">
                <xsl:for-each select="column">
                    <xsl:variable name="currentName" select="current()/name"/>
                    <xsl:variable name="currentValue" select="current()/value"/>
                    <xsl:for-each select="$currentRow/row/column">
                        <xsl:if test="current()/name=$currentName and current()/value=$currentValue">Y</xsl:if>
                        <!--<xsl:value-of select="$currentName"/>
                        <xsl:value-of select="$currentValue"/>
                        -->
                    </xsl:for-each>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

2 个答案:

答案 0 :(得分:1)

这样怎么样?

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="currentRow" select="document('C:\Personal\09086559\Desktop\Xmls\currentRow.xml')"/>

<xsl:key name="col" match="column" use="concat(name, '|', value)" />

<xsl:template match="/">
    <xsl:variable name="keyvals">
        <xsl:for-each select="$currentRow/row/column">
            <keyval><xsl:value-of select="concat(name, '|', value)" /></keyval>
        </xsl:for-each>
    </xsl:variable>
    <output>
        <xsl:choose>
            <xsl:when test="key('col', exsl:node-set($keyvals)/keyval)">yes</xsl:when>
            <xsl:otherwise>no</xsl:otherwise>
        </xsl:choose>
    </output>
</xsl:template>

</xsl:stylesheet>

编辑:

如果我理解你的澄清,即&#34;是&#34;表示至少有一行的列在另一个文档中都有匹配的列,然后尝试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="currentRow" select="document('C:\Personal\09086559\Desktop\Xmls\currentRow.xml')"/>

<xsl:template match="/">
    <xsl:variable name="keyvals">
        <xsl:for-each select="$currentRow/row/column">
            <keyval><xsl:value-of select="concat(name, '|', value)" /></keyval>
        </xsl:for-each>
    </xsl:variable>
    <output>
        <xsl:choose>
            <xsl:when test="jdbcGeneralActivityOutput/unknownResultset/row[count(column) = count(column[concat(name, '|', value)=exsl:node-set($keyvals)/keyval])]">yes</xsl:when>
            <xsl:otherwise>no</xsl:otherwise>
        </xsl:choose>
    </output>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

Vivek的原始答案似乎没问题。您可以更改for-each以将模板和修改后的输出应用于比Y更清晰的内容。程序只是比较两个XML中的所有元素 column 以查找它们是否相同。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="column">
<xsl:variable name="currantName" select="name"/>
<xsl:variable name="currantValue" select="value"/>
 <xsl:for-each select="document('currentNode.xml')//column"><!--modify your PATH to currentNode.xml-->
 <xsl:if test="(./name/text() = $currantName) and (./value/text() = $currantValue) ">
 YES, found the element
 <xsl:copy-of select="."/>
from  currentNode.xml   in keylist.xml.
 </xsl:if>
 </xsl:for-each>

</xsl:template>

<xsl:template match="row">
<root>
<xsl:apply-templates/>
Search is over. All possible matches should be listed above.
</root>
</xsl:template>
</xsl:stylesheet>