2013-08-27 91 views
0

我有這個XML條件XSLT轉換

<TreeView> 
    <Parent text="Installation"> 
    <child company="all" text="Startup" type="video" file="startup.mp4"/> 
    <child company="all" text="Getting there" type="video" file="small.mp4"/> 
    <child company="all" text="Steps" type="pdf_document" file="test.pdf"/> 
    <child company="all" text="Pictures" type="presentation" file="pics.ppx"/> 
    </Parent> 
    <Parent text="Usage"> 
    <child company="B" text="Tilbud pane" type="video" file="b1.mp4"/> 
    <child company="B" text="Report pane" type="pdf_document" file="b2.pdf"/> 
    <child company="N" text="Tilbud pane" type="video" file="n1.mp4"/> 
    <child company="N" text="Report pane" type="pdf_document" file="n2.pdf"/> 
    <child company="D" text="Tilbud pane" type="video" file="d1.mp4"/> 
    <child company="D" text="Report pane" type="pdf_document" file="d2.pdf"/> 
    </Parent> 
</TreeView> 

XSLT至今:

<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> 
      <a href="{@file}" class="video"> 
      <img src="play_icon.png" alt="video" title="Video tutorial"/> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="@text"/> 
      </a> 
     </li> 
    </xsl:template> 
    <xsl:template match="child[@type='pdf_document']"> 
     <li> 
      <a href="{@file}" class="pdfdoc"> 
      <img src="pdf_icon.png" alt="pdfdoc" title="PDF Document"/> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="@text"/> 
      </a> 
     </li> 
    </xsl:template> 
    <xsl:template match="child[@type='presentation']"> 
     <li><a href="{@file}" class="presentation"> 
      <img src="powerpoint_icon.png" alt="presentation" title="Power Point presentation"/> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="@text"/> 
      </a> 
     </li> 
    </xsl:template> 
</xsl:stylesheet> 

它可以正常使用,如預期,但我想補充一個功能的轉變。我想要取決於公司屬性的值,以包含整個元素,或者跳過它。

闡述:的子元素,其公司屬性值「所有」,必須包含在轉換文件。之後,子元素的其餘部分只能在具有公司屬性值「B」的情況下進行分組。然後,我將爲不同的公司提供3個不同的XSL文件。所以我現在只需要一個公司的XSL代碼。

我不確定我是否必須以某種方式使用條件語句或模板。我只是被竊聽了,因爲我的XSL文件對我來說變得有點複雜。

如果有人可以根據我的要求進行添加,現有的代碼 - 將不勝感激。

+0

你好Milknocookiez,您可以將XPATH添加到您的過濾元素:的。這隻會給你想要的子元素。但他們應該如何分組?你有沒有看過?最好的問候,PETER – Peter

+0

@彼得 - 謝謝。他們應該像以前一樣分組。我需要的唯一補充是區分* company *屬性。所以如果屬性是** all **或** B ** - 他們會被分組,其餘的都會被跳過。 – Milkncookiez

回答

1

所以,如果我用這個源XML:

<TreeView> 
<Parent text="Installation"> 
    <child company="all" text="Startup" type="video" file="startup.mp4"/> 
    <child company="all" text="Getting there" type="video" file="small.mp4"/> 
    <child company="all" text="Steps" type="pdf_document" file="test.pdf"/> 
    <child company="all" text="Pictures" type="presentation" file="pics.ppx"/> 
</Parent> 
<Parent text="Usage"> 
    <child company="B" text="Tilbud pane" type="video" file="b1.mp4"/> 
    <child company="B" text="Report pane" type="pdf_document" file="b2.pdf"/> 
    <child company="N" text="Tilbud pane" type="video" file="n1.mp4"/> 
    <child company="N" text="Report pane" type="pdf_document" file="n2.pdf"/> 
    <child company="D" text="Tilbud pane" type="video" file="d1.mp4"/> 
    <child company="D" text="Report pane" type="pdf_document" file="d2.pdf"/> 
</Parent> 

,並應用此XSLT:

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

<xsl:output indent="yes" omit-xml-declaration="yes"/> 
<xsl:strip-space elements="*"/> 

<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[@company='all' or @company='B']"/> 
     </ul> 
    </li> 
</xsl:template> 

<xsl:template match="child[@type='video']"> 
    <li> 
     <a href="{@file}" class="video"> 
      <img src="play_icon.png" alt="video" title="Video tutorial"> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="@text"/> 
      </img> 
     </a> 
    </li> 
</xsl:template> 
<xsl:template match="child[@type='pdf_document']"> 
    <li> 
     <a href="{@file}" class="pdfdoc"> 
      <img src="pdf_icon.png" alt="pdfdoc" title="PDF Document"> 
      <xsl:text> </xsl:text> 
      <xsl:value-of select="@text"/> 
      </img> 
     </a> 
    </li> 
</xsl:template> 
<xsl:template match="child[@type='presentation']"> 
    <li> 
     <a href="{@file}" class="presentation"> 
     <img src="powerpoint_icon.png" alt="presentation" title="Power Point presentation"> 
     <xsl:text> </xsl:text> 
     <xsl:value-of select="@text"/> 
     </img> 
     </a> 
    </li> 
</xsl:template> 
</xsl:stylesheet> 

你會得到這樣的輸出:

<ul class="LinkedList" id="LinkedList1"> 
<li>Installation<br/> 
    <ul> 
     <li> 
      <a class="video" href="startup.mp4"> 
       <img title="Video tutorial" alt="video" src="play_icon.png"> Startup</img> 
      </a> 
     </li> 
     <li> 
      <a class="video" href="small.mp4"> 
       <img title="Video tutorial" alt="video" src="play_icon.png"> Getting there</img> 
      </a> 
     </li> 
     <li> 
      <a class="pdfdoc" href="test.pdf"> 
       <img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Steps</img> 
      </a> 
     </li> 
     <li> 
      <a class="presentation" href="pics.ppx"> 
       <img title="Power Point presentation" alt="presentation" 
        src="powerpoint_icon.png"> Pictures</img> 
      </a> 
     </li> 
    </ul> 
</li> 
<li>Usage<br/> 
    <ul> 
     <li> 
      <a class="video" href="b1.mp4"> 
       <img title="Video tutorial" alt="video" src="play_icon.png"> Tilbud pane</img> 
      </a> 
     </li> 
     <li> 
      <a class="pdfdoc" href="b2.pdf"> 
       <img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Report pane</img> 
      </a> 
     </li> 
    </ul> 
</li> 
</ul> 

考慮<xsl:output indent="yes" omit-xml-declaration="yes"/>在裏面樣式表。在原始樣式表中,您有output=html,這會導致您的<br/>元素「鬆動」結束標記。

我希望輸出如你所料。

最好的問候, 彼得

+0

它像魔術般運作!謝謝。 我只是不明白帶'
'的零件丟失了它們的結束標記。當他們「放鬆」它會發生什麼? – Milkncookiez

+0

根據XHTML標準,每個標籤都需要一個結束標籤或一個自動結束標籤。只是爲了安全起見,我會一直確保br元素具有自閉標籤。最好的問候,彼得 – Peter