2013-07-08 62 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> 

(和注释,用于测试目的不同的用户),它返回我所需的输出。