2013-07-08 89 views
0

我有示例XML這樣的:選擇排序XML節點基於某些條件

User 
    name A 
    Id  8 

User 
    name B 
    Id  5 

User 
    name C 
    Id  16 

User 
    name D 
    Id  10 
... 

這是我所期望的,如果輸入的是隻有

  • A - >輸出應該是A 。只有

  • 乙 - >輸出應爲B.

  • 甲乙僅 - >輸出應該是B.

  • 除A和B之外的任何用戶 - >輸出都應該是具有最低ID的用戶的名稱。

  • A,B和其他用戶 - >與除最低ID和用戶B.

我曾嘗試下面的模板

<xsl:template name="getPriorityName"> 
     <xsl:for-each select="//*[local-name()='User']"> 
      <xsl:sort select="./*[local-name()='Id']" data-type="number"/> 
      <xsl:variable name="name"> 
       <xsl:value-of select="./*[local-name()='name']"/> 
      </xsl:variable> 
      <xsl:choose> 
       <xsl:when test="$name!='A' and $name!='B'"> 
        <xsl:if test="position()=1"> 
         <xsl:value-of select="$name"/> 
        </xsl:if> 
       </xsl:when> 
      </xsl:choose>  
     </xsl:for-each> 
</xsl:template> 

不同的變種但由於我無法以俱樂部名稱條件與每個它不工作的情況下,當A和B有輸入

謝謝Jirka :) 我修改了tempalte一點點現在看起來不錯:

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

    <xsl:template match="/Users"> 
     <xsl:variable name="UsersCount" select="count(User)" /> 
     <xsl:variable name="UsersExceptAB" select="User[Name != 'A' and Name !='B']" /> 
     <xsl:variable name="UsersAB" select="User[Name = 'A' or Name ='B']" /> 
     <xsl:variable name="UserInABWithMinimumId" select="$UsersAB[not($UsersAB/Id &lt; ./Id)]" /> 
     <xsl:variable name="UserExceptABWithMinimumId" select="$UsersExceptAB[not($UsersExceptAB/Id &lt; ./Id)]" /> 

     <result>   
      <xsl:choose> 
       <xsl:when test="$UsersExceptAB"> 
        <xsl:copy-of select="$UserExceptABWithMinimumId" /> 
       </xsl:when> 
       <xsl:when test="$UsersAB"> 
        <xsl:copy-of select="$UserInABWithMinimumId" /> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:comment>Oops, something is wrong.</xsl:comment> 
       </xsl:otherwise> 
      </xsl:choose> 
     </result> 
    </xsl:template> 

</xsl:stylesheet> 

有沒有一種方法,我可以只將名稱分配給一個變量;讓我們說; priorityName,我以後可以使用,我有一些其他的邏輯基於這個priorityName在我的應用程序。我嘗試從節點訪問名稱,但由於我們最終擁有的是節點集(始終爲1號大小 - UserInABWithMinimumId或UserExceptABWithMinimumId)。這是我試過的:

<xsl:variable name="priorityName"> 
      <xsl:choose> 
       <xsl:when test="$UsersExceptAB"> 
        <xsl:value-of select="$UserExceptABWithMinimumId/User/Name" /> 
       </xsl:when> 
       <xsl:when test="$UsersAB"> 
        <xsl:value-of select="$UserInABWithMinimumId/User/Name" /> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:comment>Oops, something is wrong.</xsl:comment> 
       </xsl:otherwise> 
      </xsl:choose> 
      </xsl:variable> 

謝謝!!!

+0

嗨,肯,我添加了模板,我試過這個問題 – Praveen

+0

是的,你應該能夠在變量中存儲用戶名。你非常接近,只要將'''中的xpath更改爲'''和''一樣,假設你的變量中也可以有多個用戶,所以你也可以像'$ UserExceptABWithMinimumId [1]/Name '只是爲了確保一個名字存儲在你的變量中 –

+0

謝謝,我得到了這個名字,但是我得到的變量是一個節點片段,我可以將它改變爲字符串嗎? – Praveen

回答

0

你可以試試下面的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="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/Users"> 
     <xsl:variable name="UsersCount" select="count(User)" /> 
     <xsl:variable name="UsersExceptAB" select="User[Name != 'A' and Name !='B']" /> 
     <xsl:variable name="UsersExceptABWithMinimumId" select="$UsersExceptAB[not($UsersExceptAB/Id &lt; ./Id)]" /> 

     <result>   
      <xsl:choose> 
       <!-- There exists some User with other name than A or B--> 
       <xsl:when test="$UsersExceptAB"> 
        <xsl:copy-of select="$UsersExceptABWithMinimumId" /> 
       </xsl:when> 
       <!-- There exists no User with other than A or B but there is a User with name B --> 
       <xsl:when test="User[Name = 'B']"> 
        <xsl:copy-of select="User[Name = 'B']" /> 
       </xsl:when> 
       <!-- There is no User with other than A --> 
       <xsl:when test="User[Name='A']"> 
        <xsl:copy-of select="User[Name = 'A']" /> 
       </xsl:when> 
       <!-- Probably there is no User --> 
       <xsl:otherwise> 
        <xsl:comment>Oops, something is wrong.</xsl:comment> 
       </xsl:otherwise> 
      </xsl:choose> 
     </result> 
    </xsl:template> 

</xsl:stylesheet> 

當我用這個輸入

<?xml version="1.0" encoding="UTF-8"?> 
<Users> 
    <User> 
     <Name>A</Name> 
     <Id>8</Id> 
    </User> 
    <User> 
     <Name>B</Name> 
     <Id>9</Id> 
    </User> 
    <User> 
     <Name>C</Name> 
     <Id>6</Id> 
    </User> 
    <User> 
     <Name>D</Name> 
     <Id>10</Id> 
    </User> 
     <User> 
     <Name>E</Name> 
     <Id>11</Id> 
    </User> 
</Users> 

(和註釋,用於測試目的不同的用戶),它返回我所需的輸出。