通过XSL中的另一个属性获取属性



我讨厌XSLT,所以我不知道如何处理它。。。我从EQ2数据库得到一个提要。XML如下所示:

<guilds limit="1" returned="1">
  <guild accounts="3" alignment="0" dateformed="1127855265" guildid="111" guildstatus="0" id="1111111111" last_update="1326986410" level="11" name="MyGuild" version="1" world="Permafrost" worldid="202">
    <ranks>
      <rank id="0" name="Leader"/>
      <rank id="1" name="Senior Officer"/>
      <rank id="2" name="Officer"/>
      <rank id="3" name="Senior Member"/>
      <rank id="4" name="Member"/>
      <rank id="5" name="Junior Member"/>
      <rank id="6" name="Initiate"/>
      <rank id="7" name="Recruit"/>
    </ranks>
    <members>
      <member dbid="123456" rank="0" name="Dude1"/>
      <member dbid="123457" rank="1" name="Dude2"/>
      <member dbid="123458" rank="2" name="Dude3"/>
      <member dbid="123459" rank="4" name="Dude4"/>
      <member dbid="123460" rank="4" name="Dude5"/>
      <member dbid="123461" rank="4" name="Dude6"/>
    </members>
    <events/>
  </guild>
</guilds>

我正试着把排名加在一张表里。它的XSL(版本1)片段如下,但从排名中添加名称无法正常工作——我知道这部分是错误的:

<xsl:value-of select="rank/rank[@id=1]/@name"/>

那么,我能得到一些帮助让它发挥作用吗?也许我能想到如何缩短它?

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
<xsl:template match="/">
<table width="100%" cellspacing="0" cellpadding="0" id="eq2roster" align="center">
 <thead>
  <tr>
   <td colspan="4" class="eq2CharacterHeader">Character</td>
   <td colspan="3" class="eq2TradeskillsHeader">Tradeskills</td>
  </tr>
  <tr class="ForumCategoryHeader">
   <th class="eq2NameHeader">Name</th>
   <th class="eq2RankHeader">Rank</th>
   <th class="eq2ClassHeader">Class</th>
   <th class="eq2LevelHeader">Level</th>
   <th class="eq2ArtisanHeader">Artisan</th>
   <th class="eq2ArtisanLevelHeader">Level</th>
   <th class="eq2SecondaryHeader">Secondary</th>
  </tr>
 </thead>
 <tbody>
  <xsl:for-each select="guilds/guild/members/member">
   <xsl:if test="@id &gt; 1">
   <tr>
    <td class="eq2Name">
     <xsl:element name="a">
      <xsl:attribute name="href">
       <xsl:text>http://eq2players.station.sony.com/</xsl:text>
       <xsl:value-of select="concat(normalize-space(/guilds/guild/@world), '/')"/>
       <xsl:value-of select="concat(normalize-space(@name), '/')"/>
      </xsl:attribute>
      <xsl:attribute name="target">
       <xsl:text>_blank</xsl:text>
      </xsl:attribute>
      <xsl:value-of select="normalize-space(@name)"/>
     </xsl:element>
    </td>
    <xsl:element name="td">
     <xsl:attribute name="class">
      <xsl:text>eq2Rank eq2rank-</xsl:text>
      <xsl:value-of select="translate(normalize-space(guild/@rank),' ','')"/>
     </xsl:attribute>
       <xsl:choose>
         <xsl:when test="member/@rank = 1"><span class = "rank1"><xsl:value-of select="ranks/rank[@id=1]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 2"><span class = "rank2"><xsl:value-of select="ranks/rank[@id=2]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 3"><span class = "rank3"><xsl:value-of select="ranks/rank[@id=3]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 4"><span class = "rank4"><xsl:value-of select="ranks/rank[@id=4]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 5"><span class = "rank5"><xsl:value-of select="ranks/rank[@id=5]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 6"><span class = "rank6"><xsl:value-of select="ranks/rank[@id=6]/@name"/></span></xsl:when>
         <xsl:when test="member/@rank = 7"><span class = "rank7"><xsl:value-of select="ranks/rank[@id=7]/@name"/></span></xsl:when>
        </xsl:choose>
    </xsl:element>
    <xsl:element name="td">
     <xsl:attribute name="class">
      <xsl:text>eq2Class eq2</xsl:text>
      <xsl:value-of select="translate(normalize-space(type/@class),' ','')"/>
     </xsl:attribute>
    <xsl:value-of select="type/@class"/>
    </xsl:element>
    <td class="eq2level"><xsl:value-of select="type/@level"/></td>
    <xsl:element name="td">
     <xsl:attribute name="class">
      <xsl:text>eq2ArtisanClass eq2</xsl:text>
      <xsl:value-of select="translate(normalize-space(tradeskills/tradeskill/@class),' ','')"/>
     </xsl:attribute>
    <xsl:value-of select="tradeskills/tradeskill/@class"/>
    </xsl:element>
    <td class="eq2ArtisanLevel"><xsl:value-of select="tradeskills/tradeskill/@level"/></td>
    <xsl:element name="td">
     <xsl:attribute name="class">
      <xsl:text>eq2Secondary eq2</xsl:text>
      <xsl:value-of select="translate(normalize-space(secondarytradeskills/secondarytradeskill/@name),' ','')"/>
     </xsl:attribute>
    <xsl:value-of select="secondarytradeskills/secondarytradeskill/@name"/>
    </xsl:element>
   </tr>
   </xsl:if>
  </xsl:for-each>
 </tbody>
</table>
</xsl:template>
</xsl:stylesheet>

以下表达式使用current()member与其对应的rank关联起来(假设上下文节点是所讨论的member):

../../ranks/rank[@id=current()/@rank]/@name

完整示例:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="member">
        <td class="eq2Rank eq2rank-{guild/@rank}">
            <span class="rank{@rank}">
                <xsl:value-of 
                    select="../../ranks/rank[@id=current()/@rank]/@name"/>
            </span>
        </td>
    </xsl:template>
</xsl:stylesheet>

输出(基于最初发布的示例XML):

<td class="eq2Rank eq2rank-">
   <span class="rank0">Leader</span>
</td>
<td class="eq2Rank eq2rank-">
   <span class="rank1">Senior Officer</span>
</td>
<td class="eq2Rank eq2rank-">
   <span class="rank2">Officer</span>
</td>
<td class="eq2Rank eq2rank-">
   <span class="rank4">Member</span>
</td>
<td class="eq2Rank eq2rank-">
   <span class="rank4">Member</span>
</td>
<td class="eq2Rank eq2rank-">
   <span class="rank4">Member</span>
</td>

不确定你是否能做这样的事情,但这可能值得一试。在单一成员的情况下

<xsl:value = "//ranks/rank[@id = ./@rank]/@name"/>

它使用X-Path跳到顶部并查找排名节点,找到ID属性等于成员排名属性的排名元素,并获取名称属性

我已经有几年没有这样的XSL了,但应该允许您摆脱XSL:选择块

我真的不确定是否有问题。

假设你有合适的rankid,你可能想使用可变

<xsl:variable name="rank" select="translate(normalize-space(guild/@rank),' ','')"/>
<span class = "rank{$rank}"><xsl:value-of select="ranks/rank[@id = $rank]/@name"/></span>

相关内容

  • 没有找到相关文章

最新更新