2013-08-21 2 views
1

XML 파일의 type 속성 값에 따라 "switch"종류의 조건문을 작성하려고합니다. 특정 아이콘 HTML에서 다음 텍스트를 추가합니다. 하지만 완전히 작동하지 않는다면 ... 또는 정확히 원하는대로하지 못합니다.XSLT의 "Choose-when"조건문

는 XML은 :

<?xml version="1.0" encoding="utf-8"?> 
<TreeView> 
    <Parent text="Installation"> 
    <child text="Startup" type="video" file="startup.wmv" icon="c://user/desktop/blahblah.jpg"/> 
    <child text="Getting there" type="video" file="gettingthere.wmv" icontPath="something"/> 
    <child text="Steps" type="document" file="steps.docx" icon="asd"/> 
    <child text="Pictures" type="presentation" file="pics.jpg" icon="dasdasdas"/> 
    </Parent> 
    <Parent text="Usage"> 
    <child text="Tilbud pane" type="video" file="tilbud.mwv" icon="asdasd"/> 
    <child text="Report pane" type="document" file="report.docx" icon="gfdhd"/> 
    </Parent> 
</TreeView> 

XSLT는 :

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

    <xsl:output method="html" encoding="utf-8"/> 

    <xsl:template match="/"> 
     <ul id="LinkedList1" class="LinkedList"> 
     <xsl:apply-templates/> 
     </ul> 
    </xsl:template> 

    <xsl:template match="Parent"> 
     <li> 
     <xsl:value-of select="@text"/> 
     <br/> 
     <ul> 
      <xsl:choose> 
      <xsl:when test="child/@type = 'video'"> 
       <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" /> 
      </xsl:when> 
      <xsl:when test="child/@type = 'document'"> 
       <img src="word_icon.png" alt="text_document_icon" title="Text Document" /> 
      </xsl:when> 
      <xsl:when test="child/@type = 'presentation'"> 
       <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point presentation" /> 
      </xsl:when> 
      </xsl:choose> 
      <xsl:apply-templates select="child"/> 
     </ul> 
     </li> 
    </xsl:template> 

    <xsl:template match="child"> 
     <li><xsl:value-of select="@text"/></li> 
    </xsl:template> 

</xsl:stylesheet> 

이것은 내가 무엇을 얻을 ->

![Not correct.][1] 
------------------------------------ 

그리고 이것은 내가 그것을 할 방법입니다 모양 ->

![Correct.][2] 
------------------------------------ 

나는 조건문을 올바르게 주문하지는 않겠지 만 실마리가 없습니다. 그래서, 어떤 생각을 어떻게 해결할 수 있을까요?

답변

2

은 당신의 접근 방식이 하나

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

    <xsl:output method="html" encoding="utf-8"/> 

    <xsl:template match="/"> 
     <ul id="LinkedList1" class="LinkedList"> 
     <xsl:apply-templates/> 
     </ul> 
    </xsl:template> 

    <xsl:template match="Parent"> 
     <li> 
      <xsl:value-of select="@text"/> 
      <br/> 
      <ul> 
       <xsl:apply-templates select="child"/> 
      </ul> 
     </li> 
    </xsl:template> 

    <xsl:template match="child[@type='video']"> 
     <li> 
      <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" /> 
      <xsl:value-of select="@text"/> 
     </li> 
    </xsl:template> 
    <xsl:template match="child[@type='document']"> 
     <li> 
       <img src="word_icon.png" alt="text_document_icon" title="Text Document" /> 
      <xsl:value-of select="@text"/> 
     </li> 
    </xsl:template> 
    <xsl:template match="child[@type='presentation']"> 
     <li> 
      <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point presentation" /> 
      <xsl:value-of select="@text"/> 
     </li> 
    </xsl:template> 
</xsl:stylesheet> 

문제는 그냥 각 부모에 대해 하나의 <IMG>을 생성한다는 것입니다보십시오.

당신은 당신이 또한 이유 없다 생각했다 (I는 "부모"템플릿에서 '선택 - when` 문을 넣어 것입니다)이

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

    <xsl:output method="html" encoding="utf-8"/> 

    <xsl:template match="/"> 
     <ul id="LinkedList1" class="LinkedList"> 
     <xsl:apply-templates/> 
     </ul> 
    </xsl:template> 

    <xsl:template match="Parent"> 
     <li> 
      <xsl:value-of select="@text"/> 
      <br/> 
      <ul> 
       <xsl:apply-templates select="child"/> 
      </ul> 
     </li> 
    </xsl:template> 

    <xsl:template match="child"> 
     <li> 
      <xsl:choose> 
       <xsl:when test="@type = 'video'"> 
        <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" /> 
       </xsl:when> 
       <xsl:when test="@type = 'document'"> 
        <img src="word_icon.png" alt="text_document_icon" title="Text Document" /> 
       </xsl:when> 
       <xsl:when test="@type = 'presentation'"> 
        <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point presentation" /> 
       </xsl:when> 
      </xsl:choose> 
      <xsl:value-of select="@text"/> 
     </li> 
    </xsl:template> 
</xsl:stylesheet> 
+0

그래, 난 그냥 깨달았다 내 실수를 사용할 수 있습니다 선호하는 경우 자식 템플릿에서'choose-when'을 사용하십시오. 그런 다음 대답을 업데이트했습니다. :) 감사! – Syspect

+0

+1을 사용하는 대신 '' –