2012-06-10 5 views
0

가 여기에 XML 파일의 선택 :XML 및 XSLT 적용-템플릿

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 



<xsl:template match="root"> 
<html><head></head> 
<body> 
Start of root in XSLT <p/> <xsl:apply-templates select="person" /> <p/> 

End of root in XSLT 

</body> 
</html> 
</xsl:template> 



<xsl:template match="shop"> 
"Step 1 start" 
<xsl:apply-templates select="*/*"/> 
"Step 1 done" <p/> 
</xsl:template> 



<xsl:template match="employee"> 
<u> <xsl:apply-templates select="name"/> </u> 
(Task: <xsl:apply-templates select="task"/>) 
<br></br> 
</xsl:template> 


</xsl:stylesheet> 

출력은 다음과 같습니다 : 루트의

시작 XSLT

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="2.xsl" ?> 


<root> 
    <shop> 
     <person> 
      <employee> 
       <name> Alexis </name> 
       <role> Manager </role> 
       <task> Sales </task> 
      </employee> 

      <employee> 
       <name> Employee2 </name> 
      </employee> 
     </person> 

     <employee> 
      <name> Blake2 </name> 
     </employee> 

    </shop> 



    <person> 
     <employee2> 
      <role2> Supervisor </role2> 
      <name2> Blake </name2> 
      <task2> Control </task2> 
     </employee2> 
    </person> 
</root> 

여기에 XSLT 파일입니다

감독자 블레이크 컨트롤

XSLT에서 루트

끝, 왜 출력의 알렉시스와 Employee2 일부가 아닙니다

내 질문? 그들은 둘 다 <person> 요소 아래에 있습니다 .....

답변

0

<xsl:template match="root"><root> 요소에 적용됩니다. 여기에서 <xsl:apply-templates select="person" />은 현재 노드 (루트 요소 <root>)의 직계 하위 요소 인 <person> 요소를 선택합니다. <person>에 대해 정의 된 템플릿이 없으므로 XSLT의 기본 동작이 실행됩니다. 즉, 텍스트 노드 만 복사됩니다. (문서 자체를 기준으로) 루트 레벨에 더 <shop> 또는 <employee> 요소가 없기 때문에

는 다른 템플릿은 전혀 실행되지 않으며, 템플릿은 하나, <apply-templates> 통해 호출되지 않습니다.

첫 번째 <person> 요소가 다른 요소에 중첩되어 있기 때문에 선택되지 않습니다, 더 명시 적으로 귀하의 질문에 대답하기 위해,하지만 <apply-templates> 명령은 현재 노드에서 직접 볼 수 있습니다 <person> 요소를 선택합니다.

+0

고맙습니다. 지금은 이해합니다! – shadowz1337

0

그러나 단지 을 원하지 않으시는 분은 사람이십니까? 당신이 할 수 있지만 그 말을하는 방법을 모를 경우 <xsl:apply-templates select="//person" />