0

機能する XSLT 条件を設定するには、助けが必要です。質問ノードを実行する for-each ループ内。ノード A の条件が機能する場合、ノード B に詳細を表示します。XML の例:

<Survey>
    <questions>
        <Number>1.0</Number>
        <Question>Was the show good?</Question>
        <Answer>Yes/No</Answer>
        <Details>N/A</Details>
    </questions>
    <questions>
        <Number>1.1</Number>
        <Question>Explain why</Question>
        <Answer>N/A</Answer>
        <Details>The actors were good</Details>
    </questions>
    <questions>
        <Number>2.0</Number>
        <Question>Was the food good?</Question>
        <Answer>Yes|No</Answer>
        <Details>N/A</Details>
    </questions>
    <questions>
        <Number>2.1</Number>
        <Question>Provide details</Question>
        <Answer>N/A</Answer>
        <Details>The pasta was too salty</Details>
    </questions>
</Survey>

必要なのは、for-each のループのみを使用することです 質問番号 = 1.0 および回答 = 'はい' の場合、質問番号 1.1 のみに詳細を表示します 質問番号 = 2.0 および回答 = 'いいえ' の場合、質問番号 2.1 に詳細を表示します

if, for each, choose/when など、あらゆる方法を試しましたが、うまくいきませんでした。他の投稿を確認しましたが、同様のものは見つかりませんでした。ツビさん、よろしくお願いします。

申し訳ありませんが、次のようにする必要があります。

    <Survey>     
        <questions>
            <Number>1.0</Number>
            <Question>Was the show good?</Question>
            <Answer>Yes</Answer>
            <Details>N/A</Details>     
    </questions>     
    <questions>
         <Number>1.1</Number>
         <Question>Explain why</Question>
         <Answer>N/A</Answer>         
         <Details>The actors were good</Details>
     </questions>     
     <questions>
         <Number>2.0</Number>
         <Question>Was the food good?</Question>         
         <Answer>Yes</Answer>
         <Details>N/A</Details>
     </questions>
     <questions>
         <Number>2.1</Number>
         <Question>Provide details</Question>
         <Answer>N/A</Answer>
         <Details>The pasta was too salty</Details>
     </questions>
 </Survey> 

これが私のコードの一部です: (申し訳ありませんが、実際には他の誰かのコードです。前の質問に「はい」または「いいえ」と明確に回答されたときに次の質問を表示する部分を変更する必要があります

<fo:table-body start-indent="0pt">
    <xsl:for-each select="../Survey">
        <xsl:for-each select="questions">
        <xsl:sort select="Number" data-type="number" order="ascending"/>
            <fo:table-row>
                <xsl:choose>
                    <xsl:when test="ends-with(Number,'0')">
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                       <fo:block text-align="right">
                                <xsl:for-each select="Number"> ...
                           </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                            <fo:block text-align="left">
                                <xsl:for-each select="Description"> ...
                                </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center"> 
                            <fo:block text-align="left" color="blue">
                               <xsl:for-each select="Answer"> ...
                               </xsl:for-each>
                            </fo:block>
                        </fo:table-cell>                                   </xsl:when>  
                    <xsl:when test="Number='1.1'">
                        <xsl:if test="//questions/Number='1.0' and //questions/Answer='No'">
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                               <fo:block text-align="right">
                                    <xsl:for-each select="Number"> ...
                                    </xsl:for-each>
                               </fo:block>
                           </fo:table-cell>
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
                               <fo:block text-align="left">
                                   <xsl:for-each select="Question">  .....
                                   </xsl:for-each>
                               </fo:block>
                           </fo:table-cell>
                           <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center"> 
                              <fo:block text-align="left" color="blue">  ....
                                   <xsl:for-each select="Details">
                                   </xsl:for-each>
                              </fo:block>
                           </fo:table-cell>                                      </xsl:if>  
                       </xsl:when>  
                       <xsl:otherwise>do something</xsl:otherwise>                        </xsl:choose>
               </fo:table-row>
            </xsl:if>
        </xsl:for-each>
    </xsl:for-each>
</fo:table-body>            
4

2 に答える 2

2

経験則として。コードに多くの重複が含まれている場合は、間違っています。XSLT も例外ではありません。

<!-- index followup questions (no zero after the dot) -->
<xsl:key 
  name="kFollowupQuestions"
  match="questions[number(substring-after(Number, '.')) != 0]" 
  use="number(substring-before(Number, '.'))"
/>

<!-- the <Survey> becomes the <fo:table-body> -->
<xsl:template match="Survey">
  <fo:table-body start-indent="0pt">
    <!-- only select main questions (with a zero after the dot) -->
    <xsl:apply-templates select="questions[number(substring-after(Number, '.')) = 0]">
      <xsl:sort select="Number" data-type="number" order="ascending" />
    </xsl:apply-templates>
  </fo:table-body>
</xsl:template>

<!-- each <questions> becomes a <fo:table-row> -->
<xsl:template match="questions">
  <xsl:variable name="qNum"   select="number(substring-before(Number, '.'))" />
  <xsl:variable name="subNum" select="number(substring-after(Number, '.'))" />
  <fo:table-row>
    <xsl:apply-templates select="Number" />
    <xsl:apply-templates select="Question" />
    <xsl:apply-templates select="Answer" />
    <xsl:apply-templates select="Details" />
  </fo:table-row>
  <!-- for main question answered with 'Yes', display followup-questions --> 
  <xsl:if test="Answer = 'Yes' and $subNum = 0">
    <xsl:apply-templates select="key('kFollowupQuestions', $qNum)">
      <xsl:sort select="Number" data-type="number" order="ascending" />
    </xsl:apply-templates>
  </xsl:if>
</xsl:template>

<!-- the <questions> children become <fo:table-cell>s -->
<xsl:template match="questions/*">
  <fo:table-cell border="solid 1pt gray" padding="1pt" display-align="center">
    <fo:block text-align="right">
      <xsl:value-of select="." /><!-- or whatever -->
    </fo:block>
  </fo:table-cell>
</xsl:template>

http://www.xmlplayground.com/K4VHM2

于 2012-08-10T20:19:32.610 に答える
1

私はあなたを変えます

<xsl:when test="Number='1.1'">
   <xsl:if test="//questions/Number='1.0' and //questions/Answer='No'">

<xsl:when test="Number='1.1'">
   <xsl:if test="../questions[Number = '1.0']/Answer = 'No'">

これは、どの質問についても、はいの回答またはいいえの回答のどちらに詳細を表示する必要があるかを知る一般的な方法がないことを前提としている<xsl:when>ため、*.0 の質問ごとに個別のケースがあります。

PSサンプル入力には子が1つしかないため、XSLが<xsl:for-each select="Number">各内部で使用するのは奇妙です。コンテキスト ノードを変更するだけです。多分意図は使用することですか?<fo:block><Number><questions><xsl:apply-templates select="Number" />

于 2012-08-10T19:08:36.100 に答える