0

XSLT 変数に値を再割り当てできない、または
<xsl:for-each条件付き
で XMLを終了できますか

<R>
  <A id="a">  
    <S id="a111">1</S>  
    <S id="a222">2</S>
    <S id="a333">3
      <S id="a3331">3.1</S>
      <S id="a3332">3.2</S>
      <S id="a3333">3.3</S>  
    </S>
    <S id="a444">4</S>
    <S id="a555">5</S>
    <S id="a666">6</S>
  </A>
  <A id="x">
    <S id="x111">1</S>
    <S id="x222">2</S>
    <S id="x333">3
      <S id="x3331">3.1</S>
      <S id="x3332">3.2</S>
      <S id="x3333">3.3</S>
    </S>
    <S id="x444">4</S>
    <S id="x555">5</S>
    <S id="x666">6</S>
  </A>
</R>

私のXSLT

     <select id ="S"  name="SList">
     <option>
       <xsl:attribute name="value"></xsl:attribute> Please Select S 
     </option>
     <xsl:for-each select="A[id='x']//S">

       <xsl:if test="'x3333'= @id">
         <xsl:variable name="currentS" select="true()"/>
       </xsl:if>
       <xsl:if test="'x3333'!= @id">
         <xsl:variable name="currentS" select="false()"/>
       </xsl:if>

       <xsl:if test="@currentS = false()">
         <option>
           <xsl:if test="@id = 'x3333'">
             <xsl:attribute name="selected">selected</xsl:attribute>
           </xsl:if>
           <xsl:attribute name="value">
             <xsl:value-of select="@id"/>
           </xsl:attribute>              
           <xsl:value-of select="Text"/>
         </option>
       </xsl:if>
     </xsl:for-each>

ドロップダウン リストを作成しようとしていますSが、リストに項目を含める必要があります。S現在の位置 (この例ではx3333current ) と の現在の ID を渡していAます。S最新の要素内の要素のリストのみを取得したいA

この例では、私の currentSx3333で id はAisであるため、リストxに含める必要があるのはx111, x222, x333,だけです。つまり、これらを排除する必要がありますx3331x3332

  <S id="x444">4</S>,
  <S id="x555">5</S>,
  <S id="x666">6</S>, nodes

このコードから<xsl:for-each select="A[id='x']//S">

どこでS要素のリストを取得していますAid='x'

誰かが私に解決策を提案できますか?

4

2 に答える 2

0

XSLT の変数でやりたいことを行う 1 つの方法は、変数の選択で XPath if ステートメントを使用することです。

<xsl:variable name="currentS" select=" if (@id = 'x3333') then true() else false()"/>

XPath if は、値が if-then 構造に依存する変数を実行しようとする場合に非常に便利です。しかし、これは XPath 2.0 プロセッサでのみ利用可能であり、OP は 1.0 プロセッサを使用しているようです。これはうまくいくはずです:

   <xsl:variable name="currentS" select="@id = 'x3333'"/>
于 2013-05-14T12:02:42.593 に答える
0

あなたが何をしようとしているのかははっきりしていません。
xslt へのヒントを次に示します。

属性の間違ったアクセス: <xsl:for-each select="A[id='x']//S">
属性 ID をテストするには、@id を使用<xsl:for-each select="A[@id='x']//S">
します: 変数を再定義することはできません:

<xsl:if test="'x3333'= @id">
        <xsl:variable name="currentS" select="true()"/>
</xsl:if>
<xsl:if test="'x3333'!= @id">
     <xsl:variable name="currentS" select="false()"/>
</xsl:if>

変数 currentS は、xsl:if のスコープでのみ定義されます。属性IDに応じて変数値を設定するには、これを試してください:

        <xsl:variable name="currentS">
            <xsl:choose>
                <xsl:when test="@id = 'x3333'">
                    <xsl:text>true</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>false</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

変数値の間違ったアクセス: <xsl:if test="@currentS = false()">
xslt 変数アクセスは wint$プレフィックスで行われます。

<xsl:if test="$currentS = 'false'">

あなたのxlstの私の解釈によると、これは役立つかもしれません:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="R">

        <select id ="S"  name="SList">
            <option>
                <xsl:attribute name="value"></xsl:attribute> Please Select S
            </option>
            <xsl:for-each select="A[@id='x']//S">
                <xsl:variable name="currentS">
                <xsl:choose>
                    <xsl:when test="preceding::S[@id = 'x3333']">
                        <xsl:text>true</xsl:text>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:text>false</xsl:text>
                    </xsl:otherwise>
                </xsl:choose>

            </xsl:variable>
                <xsl:if test="$currentS = 'false'">
                    <option>
                        <xsl:if test="@id = 'x3333'">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        <xsl:attribute name="value">
                            <xsl:value-of select="@id"/>
                        </xsl:attribute>
                        <xsl:value-of select="normalize-space(text())"/>
                    </option>
                </xsl:if>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

次の出力を生成します。

<?xml version="1.0"?>
<select id="S" name="SList">
    <option value="">
        Please Select S
    </option>
    <option value="x111">1</option>
    <option value="x222">2</option>
    <option value="x333">3</option>
    <option value="x3331">3.1</option>
    <option value="x3332">3.2</option>
    <option selected="selected" value="x3333">3.3</option>
</select>
于 2013-05-14T13:02:03.693 に答える