2013-08-27 93 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