2016-12-03 2 views
1

주어진 XSL 및 XML에 대해 출력 행이 하나 밖에 없습니다. for-each에는 두 개의 시퀀스가 ​​있기 때문에 출력 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> 

출력은 다음과 같다. 내가받은 것은 단 한 줄뿐입니다.
두 번째 시퀀스에 아무 것도 표시되지 않는 이유는 무엇입니까?

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

원자 값 :

내가

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

원자 값 아래로 주어진 같은 것을 표시하기를 기대

을 제거 :
노드를 제거 : 단지 거기이기 때문에이 코멘트

+0

에 귀하의 질문은 중요한 태그를 누락되었습니다. ''와'instance of'를 사용하기 때문에 XSLT-2.0 태그를 추가했습니다. 이제 완전히 새로운 질문입니다. – zx485

답변

0

입니다 선택된 시퀀스의 한 노드 :

첫 번째 comment 보다는 두 번째 mychild 요소의 첫 번째 코멘트 노드의 아이를 선택하고자하는 경우 노드
  • /myroot[1]/mychild[2]/comment[1]
    1. /myroot[1]/mychild[1]/test[1]/@testing 선택 1, # 2의 경우 0 노드

    를 선택 요소 (두 번째 mychild 요소의 하위 요소로 존재하지 않음),

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

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

    물론 $ node가 주석 노드이면 'element()'의 $ node instance는 false가됩니다. –

    +0

    @MichaelKay : 참이지만 XPath # 2 결함이 주석 노드를 선택하지 않기 때문에 해당 조건은 주석 노드에 적용되지 않습니다. (존재하는 경우 두 번째 반복을보고하는'xsl : otherwise'가 여전히 있습니다.) – kjhughes