我正在尝试使用辅助或不同 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>