0

我正在尝试使用辅助或不同 xml 文件中的数据更新以下 XML 中的元素。输入 XML 和辅助 XML 文件都具有相同数量的段。我需要在辅助 XML 的第一段中取一个值并更新 INPUT xml 中的一个元素等等。我不确定是否可以使用 XSL 来完成,任何人都可以指导我。

更具体地说,我正在尝试根据辅助 XML 的 //PDetails/PStatus/Code 和 //PDetails/PStatus/Description 值更新 INPUT XML 的值<indicator></indicator><iOSection>

以下是输入 XML 文件:

<IResponse>
    <iOSection>
        <Details>           
            <Info>
                <pNumber>FB061689</pNumber>
                <indicator></indicator>
                <Identifier>1</Identifier>
            </Info>         
        </Details>
        <Token>
            <Reference>1UUYD05BHM21OJCK3881C7F</Reference>
        </Token>
    </iOSection>
    <iOSection>
        <Details>           
            <Info>
                <pNumber>FB061690</pNumber>
                <indicator></indicator>
                <Identifier>2</Identifier>
            </Info>
        </Details>
        <Token>
            <Reference>1UUYD05BHM21OJCK3881C7F</Reference>
        </Token>
    </iOSection>
</IResponse>

下面是辅助 XML 文件 - 它在名为 RSPDetails 的 xsl 变量中可用

<RS PartID="abcd" SysID="mnc">  
    <PDetails>
        <PN>FB063586</PN>
        <PStatus>
            <Code>0</Code>
            <Description>Cancelled</Description>
        </PStatus>     
    </PDetails>
    <PDetails>
        <Error>
            <Code>92</Code>
            <Message>failed</Message>
        </Error>
    </PDetails>
</RS>

当 //PDetails/PStatus/Code = '0' 和 //PDetails/PStatus/Description = 'Cancelled' 时的值<indicator>应该是 'YES' ,在所有其他情况下它应该是 'NO'

条件应适用于<iOSection>位置 1 使用<PDetails>位置 1 数据和<iOSection>位置 2 使用<PDetails>位置 2 数据,依此类推

期望的输出是:

<IResponse>
    <iOSection>
        <Details>           
            <Info>
                <pNumber>FB061689</pNumber>
                <indicator>YES</indicator>
                <Identifier>1</Identifier>
            </Info>         
        </Details>
        <Token>
            <Reference>1UUYD05BHM21OJCK3881C7F</Reference>
        </Token>
    </iOSection>
    <iOSection>
        <Details>           
            <Info>
                <pNumber>FB061690</pNumber>
                <indicator>NO</indicator>
                <Identifier>2</Identifier>
            </Info>
        </Details>
        <Token>
            <Reference>1UUYD05BHM21OJCK3881C7F</Reference>
        </Token>
    </iOSection>
</IResponse>

我在 XSL 下尝试过,但没有更接近

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" >

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

    <xsl:template match="/*[local-name()='IResponse']/*[local-name()='iOSection']/*[local-name()='Details']/*[local-name()='Info']/*[local-name()='indicator']">

        <xsl:variable name="RSDetails">
            <RS PartID="abcd" SysID="mnc">
                <PDetails>
                    <PN>FB063586</PN>
                    <PStatus>
                        <Code>0</Code>
                        <Description>Cancelled</Description>
                    </PStatus>
                </PDetails>
                <PDetails>
                    <Error>
                        <Code>92</Code>
                        <Message>failed</Message>
                    </Error>
                </PDetails>
            </RS>       
        </xsl:variable> 

        <xsl:element name="indicator">
            <xsl:variable name="PStatus">
                <xsl:value-of select="$RSDetails/RS/PDetails/PStatus" />
            </xsl:variable>
            <xsl:variable name="Message">
                <xsl:value-of select="$RSDetails/RS/PDetails/Message" />
            </xsl:variable>

            <xsl:choose>
                <xsl:when test="$PStatus='0' and $Message='Cancelled'">
                    <xsl:value-of select="'YES'" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="'NO'" />
                </xsl:otherwise>
            </xsl:choose>

        </xsl:element>
        <xsl:copy-of select="@*" />
    </xsl:template>
</xsl:stylesheet>
4

1 回答 1

1

您在这里要做的是从外部文件中的匹配节点中查找值。您没有这么说,但我认为匹配节点是PN值与 local 匹配的节点pNumber

XSLT 1.0

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

<xsl:param name="RSPDetails" select="document('your_other_file.xml')" />
<xsl:key name="rsp" match="PDetails" use="PN" />

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

<xsl:template match="indicator">
    <xsl:copy>
        <xsl:variable name="pnum" select="../pNumber"/>
        <!-- switch context to the other file -->
        <xsl:for-each select="$RSPDetails">
            <xsl:choose>
                <xsl:when test="key('rsp', $pnum)/PStatus/Code=0 and key('rsp', $pnum)/PStatus/Description='Cancelled'">YES</xsl:when>
                <xsl:otherwise>NO</xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

编辑:

要根据位置查找,请尝试:

<xsl:template match="indicator">
    <xsl:copy>
        <xsl:variable name="i">
            <xsl:number count="iOSection"/>
        </xsl:variable>
        <xsl:variable name="detail" select="$RSPDetails/RS/PDetails[number($i)]"/>
        <xsl:choose>
            <xsl:when test="$detail/PStatus/Code=0 and $detail/PStatus/Description='Cancelled'">YES</xsl:when>
            <xsl:otherwise>NO</xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

在这种情况下,该<xsl:key>指令不是必需的。

于 2015-01-08T20:00:54.050 回答