2012-03-09 91 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