2016-12-03 1 views
1

与えられたXSLとXMLに対して、出力は1行しか得られません。 for-eachには2つのシーケンスがあるので、2行の出力が必要ですか?
私は何が欠けていますか?XSLT:なぜ<for-each>は二度反復されません

XSLを使用:使用

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    version="2.0"> 
    <xsl:output indent="yes" /> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 

    <xsl:variable name="pat" as="item()*"> 
     <xsl:sequence select="/myroot[1]/mychild[1]/test[1]/@testing"/> 
     <xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/> 
    </xsl:variable> 

    <xsl:for-each select="$pat"> 
     <xsl:choose> 
      <xsl:when test=". instance of element()"> 
       <xsl:text>  Node: </xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:text>Atomic value: </xsl:text> 
      </xsl:otherwise> 
     </xsl:choose> 
     <xsl:value-of select="."/> 
     <xsl:text>&#xA;</xsl:text> 
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

XML:

<?xml version="1.0" encoding="Windows-1252" standalone="no"?> 
<myroot > 
    <mychild id="123"> 
     <fruit>apple</fruit> 
     <test hello="world" testing="removed" brackets="angled" question="answers"/> 
     <comment>This is a comment</comment> 
    </mychild>   
    <mychild id="789"> 
     <fruit>orange</fruit> 
     <test brackets="round" hello="greeting"> 
      <number>111</number> 
     </test> 
     <!-- this is a test comment --> 
     <dates> 
      <modified>123</modified> 
      <created>880</created> 
      <accessed>44</accessed> 
     </dates> 
    </mychild>   
    <mychild id="456"> 
     <fruit>banana</fruit> 
     <comment>This will be removed</comment> 
    </mychild> 
</myroot> 

出力を以下に示します。私が受け取ったのは1行だけです。
2番目のシーケンスに何も表示されなかったのはなぜですか?

<?xml version="1.0" encoding="UTF-8"?> 

アトミック値:
ノードを削除:だけありますので、これはコメント

+0

にあなたの質問は、重要なタグがありませんでした。 ''と 'instance of'を使用しているので、私はXSLT-2.0タグを追加しました。今はまったく新しい質問です。 – zx485

答えて

0

である私は

<?xml version="1.0" encoding="UTF-8"?> 

アトミック値以下に示すように、それが何かを表示することが期待

を削除選択されたシーケンス内の1つのノード:

あなたが最初 comment のではなく、第二mychild要素の最初のコメントノードの子を選択したい場合はノード
    1. /myroot[1]/mychild[1]/test[1]/@testing選択1は、#2の場合は0のノード

    を選択します要素(2番目のmychild要素の子としては存在しません)、

    <xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/> 
    

    <xsl:sequence select="/myroot[1]/mychild[2]/comment()[1]"/> 
    
  • +1

    もちろん、$ nodeがコメントノードの場合、$ node instance of element()はfalseになります。 –

    +0

    @MichaelKay:Trueですが、その条件は、エラーのあるXPath#2がコメントノードを選択しないため、コメントノードでは決して再生されません。 (まだ存在していれば、2回目の反復を報告する 'xsl:otherwise'があります。) – kjhughes

    関連する問題