1

我希望有人可以提供帮助,我无法弄清楚,也许只是无法完成。

我有以下 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>
4

1 回答 1

0

您没有准确显示您想要的结果,但我正在键入“按原样复制”这句话......我认为您所要做的就是在处理您的属性之前复制您的属性。在 XSLT 中,您可以多次向输出元素添加一个属性节点,最后一个获胜……更早添加的节点被简单地覆盖。

所以在下面的文本中,我所做的只是先复制属性,然后处理它们,然后我取出了删除属性的模板匹配项。

t:\ftemp>type profile.xml 
<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>
t:\ftemp>call xslt2 profile.xml profile.xsl 
<ProfileRead>
    <Profile>
        <Traveler>
            <Customer GenderCode="M" RedressNumber="666" KnownTravelerNumber="123">
                <PersonName LanguageIDCode="EN-US">
                    <GivenName>John</GivenName>
                    <MiddleName>Long</MiddleName>
                    <SurName>Smith</SurName>
                    <NameSuffix>Junior</NameSuffix>
                </PersonName>

                <Document DocID="111" DocTypeCode="KAMAL"/>
                <Document DocID="222" DocTypeCode="FRANK"/>
            </Customer>
        </Traveler>
    </Profile>
</ProfileRead>

t:\ftemp>type profile.xsl 
<?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:apply-templates select="@*"/>       
            <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>

    <!-- remove element -->
    <xsl:template match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='KTID']" />
    <xsl:template match="ProfileRead/Profile/Traveler/Customer/Document[@DocTypeCode='RDNR']" />

</xsl:stylesheet>
t:\ftemp>rem Done! 
于 2013-08-10T19:00:06.250 回答