我希望有人可以提供帮助,我无法弄清楚,也许只是无法完成。
我有以下 XML,需要根据 Document 元素的条件更新 RedressNumber 和 KnownTravelerNumber。我正在使用以下 XSLT,但它不起作用。
如果 DocTypeCode 条件不成立,则应按原样复制该属性。如果条件为真,则属性值应替换为 DocID 值。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="identity.xsl" />
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="UTF-8" />
<xsl:template match="ProfileRead">
<xsl:apply-imports />
</xsl:template>
<xsl:template match="ProfileRead/Profile/Traveler/Customer">
<xsl:copy>
<xsl:for-each select="Document">
<xsl:if test="@DocTypeCode = 'KTID'">
<xsl:attribute name="KnownTravelerNumber">
<xsl:value-of select="@DocID"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@DocTypeCode = 'RDNR'">
<xsl:attribute name="RedressNumber">
<xsl:value-of select="@DocID"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ProfileRead/Profile/Traveler/Customer/@KnownTravelerNumber" />
<xsl:template match="ProfileRead/Profile/Traveler/Customer/@RedressNumber" />
<!-- remove element -->
<xsl:template match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='KTID']" />
<xsl:template match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='RDNR']" />
</xsl:stylesheet>
<ProfileRead>
<Profile>
<Traveler>
<Customer GenderCode="M" RedressNumber="321" KnownTravelerNumber="123">
<PersonName LanguageIDCode="EN-US">
<GivenName>John</GivenName>
<MiddleName>Long</MiddleName>
<SurName>Smith</SurName>
<NameSuffix>Junior</NameSuffix>
</PersonName>
<Document DocID="666" DocTypeCode="RDNR" />
<Document DocID="111" DocTypeCode="KAMAL" />
<Document DocID="222" DocTypeCode="FRANK" />
</Customer>
</Traveler>
</Profile>
</ProfileRead>