xslt - 在两个XML文件中匹配ID

时间:2017-12-22 08:49:37

标签: xml xslt

我有两个XML文件:

<results count="3">
<result item="1">
    <result>
        <fielddefinition>
            <field name="COUNT" type="Unknown" columnDisplaySize="20" precision="19" scale="0" isCurrency="false" columnTypeName="INT8" columnClassName="java.lang.Long"/>
            <field name="TASK_ID" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="INT4" columnClassName="java.lang.Integer"/>
        </fielddefinition>
        <rowset>
            <row number="0">
                <field name="COUNT">17</field>
                <field name="TASK_ID">2364</field>
            </row>
        </rowset>
    </result>
</result>
<result item="2">
<result>
    <fielddefinition>
        <field name="COUNT" type="Unknown" columnDisplaySize="20" precision="19" scale="0" isCurrency="false" columnTypeName="INT8" columnClassName="java.lang.Long"/>
        <field name="TASK_ID" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="INT4" columnClassName="java.lang.Integer"/>
    </fielddefinition>
    <rowset>
        <row number="0">
            <field name="COUNT">1</field>
            <field name="TASK_ID">3140</field>
        </row>
    </rowset>
</result>

<result>
<fielddefinition>
    <field name="NAME" type="VARCHAR" columnDisplaySize="255" precision="255" scale="0" isCurrency="false" columnTypeName="VARCHAR" columnClassName="java.lang.String"/>
    <field name="ID" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="SERIAL" columnClassName="java.lang.Integer"/>
    <field name="EFFECTIVE_FROM" type="DOUBLE" columnDisplaySize="25" precision="17" scale="17" isCurrency="false" columnTypeName="FLOAT8" columnClassName="java.lang.Double"/>
    <field name="STATUS" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="SERIAL" columnClassName="java.lang.Integer"/>
    <field name="USERID" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="INT4" columnClassName="java.lang.Integer"/>
    <field name="PRIORITY" type="INTEGER" columnDisplaySize="11" precision="10" scale="0" isCurrency="false" columnTypeName="INT4" columnClassName="java.lang.Integer"/>
</fielddefinition>
<rowset>
    <row number="0">
        <field name="NAME">qadasdasadsaaads een hele lange taak ff maken zodat ik kan zien hoe dat eruit ziet precies</field>
        <field name="ID">2364</field>
        <field name="EFFECTIVE_FROM">1513933257.97667289</field>
        <field name="STATUS">186</field>
        <field name="USERID">150</field>
        <field name="PRIORITY">0</field>
    </row>
    <row number="1">
        <field name="NAME">adads</field>
        <field name="ID">3140</field>
        <field name="EFFECTIVE_FROM">1513933432.78091598</field>
        <field name="STATUS">186</field>
        <field name="USERID">150</field>
        <field name="PRIORITY" null="true"></field>
    </row>
    <row number="2">
        <field name="NAME">dadsa</field>
        <field name="ID">3141</field>
        <field name="EFFECTIVE_FROM">1513933438.29942393</field>
        <field name="STATUS">186</field>
        <field name="USERID">150</field>
        <field name="PRIORITY" null="true"></field>
    </row>
</rowset>

其中一个是在XSLT中输入的,其中一个是XSLT中的一个参数。我想知道如何将这两个xml文件分组到他们的task_id和id上?我尝试了以下

<?xml version="1.0" encoding="UTF-8"?>

    

<xsl:param name="tasks_details"/>

<xsl:template match="/">
    <tasks>
        <xsl:apply-templates select="/result/rowset/row"/>
    </tasks>
</xsl:template>
<xsl:template match="row">
    <task>
        <name>
            <xsl:choose>
                <xsl:when test="field[@name='NAME']!=''">
                    <xsl:value-of select="field[@name='NAME']"/>
                </xsl:when>
                <xsl:otherwise> <xsl:value-of select="'-'"/> </xsl:otherwise>
            </xsl:choose>
        </name>
        <id><xsl:value-of select="field[@name='ID']"></xsl:value-of></id>
        <effective_from><xsl:value-of select="field[@name='EFFECTIVE_FROM']"></xsl:value-of></effective_from>
        <status><xsl:value-of select="field[@name='STATUS']"/></status>
        <user><xsl:value-of select="field[@name='USERID']"/></user>
        <priority><xsl:value-of select="field[@name='PRIORITY']"/></priority>
        <xsl:for-each select="$tasks_details/results/result/result/rowset/row">
            <xsl:choose>
                <xsl:when test="field[@name='TASK_ID'] = field[@name='ID'] and number(field[@name='count']) &gt; 1">
                    <created>true</created>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </task>
</xsl:template>

不确定如何连接/将这些文件组合在一起..我需要它来确定一个任务是刚刚创建的,还是已经被移动了几次......

1 个答案:

答案 0 :(得分:1)

在用于外部文件的<xsl:when>内的<xsl:for-each>条件中需要进行小的修正。由于正在对外部文件的数据执行循环,因此field[@name='ID']不会返回任何值,因为它不在外部文件中。它出现在应用XSLT的当前文件中。

您可以将值存储在变量中,然后用于匹配。

<xsl:variable name="ID" select="field[@name='ID']" />

条件修改为

<xsl:when test="$ID = field[@name = 'TASK_ID'] and number(field[@name = 'COUNT']) &gt; 1">

修改后的模板

<xsl:template match="row">
    <xsl:variable name="ID" select="field[@name='ID']" />
    <task>
        <name>
            <xsl:choose>
                <xsl:when test="field[@name='NAME']!=''">
                    <xsl:value-of select="field[@name='NAME']" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'-'" />
                </xsl:otherwise>
            </xsl:choose>
        </name>
        <id><xsl:value-of select="$ID" /></id>
        <effective_from><xsl:value-of select="field[@name='EFFECTIVE_FROM']" /></effective_from>
        <status><xsl:value-of select="field[@name='STATUS']" /></status>
        <user><xsl:value-of select="field[@name='USERID']" /></user>
        <priority><xsl:value-of select="field[@name='PRIORITY']" /></priority>
        <xsl:for-each select="$tasks_details/results/result/result/rowset/row">
            <xsl:choose>
                <xsl:when test="$ID = field[@name = 'TASK_ID'] and number(field[@name = 'COUNT']) &gt; 1">
                    <created>true</created>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </task>
</xsl:template>

输出

<tasks>
    <task>
        <name>qadasdasadsaaads een hele lange taak ff maken zodat ik kan zien hoe dat eruit ziet precies</name>
        <id>2364</id>
        <effective_from>1513933257.97667289</effective_from>
        <status>186</status>
        <user>150</user>
        <priority>0</priority>
        <created>true</created>
    </task>
    <task>
        <name>adads</name>
        <id>3140</id>
        <effective_from>1513933432.78091598</effective_from>
        <status>186</status>
        <user>150</user>
        <priority />
    </task>
    <task>
        <name>dadsa</name>
        <id>3141</id>
        <effective_from>1513933438.29942393</effective_from>
        <status>186</status>
        <user>150</user>
        <priority />
    </task>
</tasks>