2012-03-09 44 views
0

我有一个下面的代码,它从相应的plmxml文件中获取记录并显示记录。我显示license_status 0作为作者和1作为消费者。我想要的是在显示输出的同时将license_status作为作者和消费者排序?我将它显示为html。我该怎么做 ?在xslt中排序?

在此先感谢

<?xml version="1.0"?> 

<!-- 

    Filename: default_xml_template.xsl 

    Default xsl template file for XML report 

--> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:plm="http://www.plmxml.org/Schemas/PLMXMLSchema"> 

<!-- Defining output as HTML --> 
<xsl:output method="html" indent="yes"/> 

<!-- Defining Global Variables 

     These are defined to avoid redundancy and use variables throughout the xsl document 
--> 
<xsl:variable name="trvrootref" select="/plm:PLMXML/plm:Header/@traverseRootRefs"/> 
<xsl:variable name="roid" select="substring-after($trvrootref,'#')"/> 
<xsl:variable name="roe" select="/plm:PLMXML/plm:ProductView/plm:Occurrence[@id=$roid]"/> 
<xsl:variable name="rprid" select="substring-after($roe/@instancedRef,'#')"/> 
<xsl:variable name="root" select="/plm:PLMXML/plm:ProductRevision[@id=$rprid]"/> 
<!-- Reference to the Site element and last name attribute -->  
<xsl:variable name="site" select="/plm:PLMXML/plm:Site"/> 
<xsl:variable name="site_name" select="$site/@name"/> 

<!-- The match attribute is used to associate a template with an XML element. 

     The match attribute can also be used to define a template for the entire XML document. 

     The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document). 
--> 
<xsl:template match="/"> 

<!-- 

     HTML to define the structure and presentation of the output report to be published  

--> 
<html> 
<head> 
    <title>Global Teamcenter - Employee Information</title> 


<!-- 
     Calling the createCL template , passing parameter occStr as trvrootref variable 
--> 
    <xsl:call-template name="createCL"> 
     <xsl:with-param name="occStr" select="$trvrootref"/> 
    </xsl:call-template> 
    </table> 
</div> 
<br/> 
</body> 
</html> 

</xsl:template> 


<!-- Defining createCL template --> 
<xsl:template name="createCL"> 
<xsl:param name="occStr"/> 
    <xsl:if test="$occStr!=''"> 
    <xsl:choose> 
     <xsl:when test="contains($occStr,' ')"> 
      <xsl:variable name="occid" select="substring-before($occStr,' ')"/> 
      <xsl:variable name="newid" select="substring-after($occid,'#')"/> 
       <xsl:call-template name="createCL"> 
       <xsl:with-param name="occStr" select="$newid"/> 
       </xsl:call-template> 
       <xsl:call-template name="createCL"> 
       <xsl:with-param name="occStr" select="substring-after($occStr,' ')"/> 
       </xsl:call-template> 
     </xsl:when> 
<!-- inside createCL otherwise occStr <xsl:value-of select="$occStr"/> -->  
     <xsl:otherwise> 
     <xsl:choose> 
     <xsl:when test="contains($occStr,'#')"> 
      <xsl:variable name="newid" select="substring-after($occStr,'#')"/> 
<!-- 
     Calling the creCLext template , passing parameter occid as newid variable 
--> 
       <xsl:call-template name="creCLext"> 
       <xsl:with-param name="occid" select="$newid"/> 
      </xsl:call-template>     
     </xsl:when> 
      <xsl:otherwise> 
<!-- 
     Calling the creCLext template , passing parameter occid as occStr variable 
--> 
       <xsl:call-template name="creCLext"> 
        <xsl:with-param name="occid" select="$occStr"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:if> 
</xsl:template> 

<!-- Defining creCLext template --> 
<xsl:template name="creCLext"> 
<xsl:param name="occid"/> 

<!-- Reference to the user element and user_id,status and license_level attribute --> 

    <xsl:variable name="user" select="/plm:PLMXML/plm:User[@id=$occid]"/> 
    <xsl:variable name="per_ref" select="substring-after($user/@personRef,'#')" /> 
    <xsl:variable name="user_id" select="$user/@userId" /> 
    <xsl:variable name="license_level" select="$user/plm:UserData/plm:UserValue[3]/@value"/> 
    <xsl:variable name="last_login_time" select="$user/plm:UserData/plm:UserValue[4]/@value"/> 

<!-- Reference to the person element and last name attribute --> 

    <xsl:variable name="person" select="/plm:PLMXML/plm:Person[@id=$per_ref]"/>   
    <xsl:variable name="person_l" select="$person/@lastName"/> 


    <!-- Displaying the values by row order --> 
     <tr> 
     <td> 
     <xsl:value-of select="$person_l"/></td> 
     <td><xsl:value-of select="$user_id"/></td> 
     <td><xsl:choose> 
       <xsl:when test="$license_level=0">Author</xsl:when> <!-- Converting the output to string value --> 
       <xsl:otherwise>Consumer</xsl:otherwise> 
      </xsl:choose> 
     </td> 
     <td><xsl:value-of select="$last_login_time"/></td> 
     </tr>   
</xsl:template> 
</xsl:stylesheet> 
+0

此代码的基本问题是它不使用未命名的模板,相应地,'' - 所描述的“问题”是一个非常简单的模板,甚至不需要排序。我强烈建议您阅读一本关于XSLT和XPath的好书,并且在开始编写任何XSLT转换之前至少掌握基本知识。 XSLT不像C或Java,或C#,或迄今为止使用的任何语言 - 它需要“范式转​​换”,而且您显然没有通过这一点。 – 2012-03-09 14:31:45

+0

谢谢@DimitreNovatchev,叶,我是XSLT的新手,只是满足一个小的转换需求。我几乎完成了它,我需要在显示之前进行整理。是否可以按照他们的方式对元素进行排序而不会在xslt代码中进行重大更改? – kanwarpal 2012-03-10 10:11:05

回答

0

看一看xsl:sort。这是一个相当不错的example如何使用它。

+0

谢谢@werner,但我已经阅读过它,它声明我们可以在中使用排序。而我想在里面排序 kanwarpal 2012-03-09 10:15:05

+0

没有看到您的XML源代码很难解释哪里应该去。源文档中的某处会有一组您想要排序的元素。样式表中的某处会有一个xsl:for-each或xsl:apply-templates来处理这组元素。 xsl:for-each或xsl:apply-templates是xsl:sort需要去的地方。 – 2012-03-09 10:58:49

+0

@michealkay,我附上上面的xslt代码。 xsl:for-each和xsl:apply-templates没有被使用,因为该函数被重复调用。我可以对license_status进行排序吗? – kanwarpal 2012-03-10 10:15:26