0

I have the follwing data as an input:

<?xml version="1.0" encoding="UTF-8"?>
<orderbackup>
    <edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" backendriid="674256" />
    <edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" backendriid="663754" />
    <edprefinst paymentsystem="FirstData" amount="251.50000" refundmethod="VISA" backendriid="663304" />
    <ppcextdata attributename="account" searchvalue="XXXX" ppcpayinst_id="674256" />
    <ppcextdata attributename="account" searchvalue="YYYY" ppcpayinst_id="663754" />
    <ppcextdata attributename="account" searchvalue="ZZZZ" ppcpayinst_id="663304" />
</orderbackup>    

I want to end up with:

<edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" account="XXXX"/>
<edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" account="YYYY"/>
<edprefinst paymentsystem="FirstData" amount="251.50000" refundmethod="VISA" account="ZZZZ"/>

backendriid and ppcpayinst_id are the same. I want to take the searchvalue from ppcextdata and add it as account to the edprefinst by matching the backendriid to the ppcpayinst_id

I can't think of a way in xslt to make this happen. Any ideas? I'm pretty new to this, so go easy on me (for now...)

4

1 回答 1

0

这是一个选择...

XML 输入

<orderbackup>
    <edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" backendriid="674256" />
    <edprefinst paymentsystem="FirstData" amount="227.00000" refundmethod="VISA" backendriid="663754" />
    <edprefinst paymentsystem="FirstData" amount="251.50000" refundmethod="VISA" backendriid="663304" />
    <ppcextdata attributename="account" searchvalue="XXXX" ppcpayinst_id="674256" />
    <ppcextdata attributename="account" searchvalue="YYYY" ppcpayinst_id="663754" />
    <ppcextdata attributename="account" searchvalue="ZZZZ" ppcpayinst_id="663304" />
</orderbackup>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/*">
        <xsl:apply-templates select="edprefinst"/>
    </xsl:template>

    <xsl:template match="edprefinst">
        <xsl:copy>
            <xsl:apply-templates select="@*[not(name()='backendriid')]|
                following-sibling::ppcextdata[@ppcpayinst_id=current()/@backendriid]/@searchvalue"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@searchvalue">
        <xsl:attribute name="account">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

输出

<edprefinst paymentsystem="FirstData"
            amount="227.00000"
            refundmethod="VISA"
            account="XXXX"/>
<edprefinst paymentsystem="FirstData"
            amount="227.00000"
            refundmethod="VISA"
            account="YYYY"/>
<edprefinst paymentsystem="FirstData"
            amount="251.50000"
            refundmethod="VISA"
            account="ZZZZ"/>

我正在使用xsl:apply-templates而不是直接输出一个新account属性,这样如果找不到对应的属性就不会创建它ppcextdata/@searchvalue

于 2013-07-26T17:47:54.410 回答