1

誰かが助けてくれることを願っています、私はこれを理解できません、多分それはできないだけです.

次の XML があり、Document 要素の条件に基づいて RedressNumber と KnownTravelerNumber を更新する必要があります。次の XSLT を使用していますが、機能していません。

DocTypeCode 条件が true でない場合は、属性をそのままコピーする必要があります。条件が true の場合、属性値を 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 に答える