0

次のようなサンプル xml があります。

User
    name   A
    Id     8

User
    name   B
    Id     5

User
    name   C
    Id     16

User
    name   D
    Id     10
...

入力が

  • A のみ --> 出力は A である必要があります。

  • B のみ --> 出力は B である必要があります。

  • A と B のみ --> 出力は B である必要があります。

  • A と B 以外のすべてのユーザー --> 出力は、ID が最小のユーザーの名前である必要があります。

  • A、B、およびその他のユーザー --> A と B を除く ID が最も低いユーザー。

以下のテンプレートのさまざまなバリエーションを試しました

<xsl:template name="getPriorityName">
        <xsl:for-each select="//*[local-name()='User']">
            <xsl:sort select="./*[local-name()='Id']" data-type="number"/>  
            <xsl:variable name="name">
                <xsl:value-of select="./*[local-name()='name']"/>
            </xsl:variable> 
            <xsl:choose>
                <xsl:when test="$name!='A' and $name!='B'">
                    <xsl:if test="position()=1">
                        <xsl:value-of select="$name"/>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>       
        </xsl:for-each>
</xsl:template>

しかし、私は for-each で名前条件をクラブできないので、入力に A と B がある場合は機能しません

ありがとうジルカ :) テンプレートを少し変更したところ、問題ないように見えました。

<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/Users">
        <xsl:variable name="UsersCount" select="count(User)" />
        <xsl:variable name="UsersExceptAB" select="User[Name != 'A' and Name !='B']" />
        <xsl:variable name="UsersAB" select="User[Name = 'A' or Name ='B']" />
        <xsl:variable name="UserInABWithMinimumId" select="$UsersAB[not($UsersAB/Id  &lt; ./Id)]" />
        <xsl:variable name="UserExceptABWithMinimumId" select="$UsersExceptAB[not($UsersExceptAB/Id  &lt; ./Id)]" />

        <result>        
            <xsl:choose>
                <xsl:when test="$UsersExceptAB">
                    <xsl:copy-of select="$UserExceptABWithMinimumId" />
                </xsl:when>
                <xsl:when test="$UsersAB">
                    <xsl:copy-of select="$UserInABWithMinimumId" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:comment>Oops, something is wrong.</xsl:comment>
                </xsl:otherwise>
            </xsl:choose>
        </result>
    </xsl:template>

</xsl:stylesheet>

名前を変数に割り当てる方法はありますか?まあ言ってみれば; 後で使用できるpriorityName。アプリケーションには、このpriorityNameに基づく他のロジックがあります。ノードから名前にアクセスしようとしましたが、最終的にはノード セット (常にサイズ 1 - UserInABWithMinimumId または UserExceptABWithMinimumId のいずれか) であるためです。これは私が試したものです:

<xsl:variable name="priorityName">
            <xsl:choose>
                <xsl:when test="$UsersExceptAB">
                    <xsl:value-of select="$UserExceptABWithMinimumId/User/Name" />
                </xsl:when>
                <xsl:when test="$UsersAB">
                    <xsl:value-of select="$UserInABWithMinimumId/User/Name" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:comment>Oops, something is wrong.</xsl:comment>
                </xsl:otherwise>
            </xsl:choose>
            </xsl:variable>

ありがとう!!!

4

1 に答える 1