+ -
当前位置:首页 → 问答吧 → xslt中如何获得父节点的position值

xslt中如何获得父节点的position值

时间:2004-07-09

来源:互联网

xml 结构如下:
<?xml version="1.0" encoding="GB2312"?>
<MEETINGROOMINFO>
  <MEETINGROOMLIST>
    <MEETINGROOM ID="A1" NAME="A1" TIME="2004-06-25 15:30-17:30" PRICE="3000" PRESONAMOUNT="50" REMARK="1111111">
      <MEETINGITEM ID="1" NAME="笔1" AMOUNT="30" PRICE="120.5" REMARK="1111111111"/>
      <MEETINGITEM ID="2" NAME="电脑1" AMOUNT="30" PRICE="120.5" REMARK="1222222222222222"/>
    </MEETINGROOM>
    <MEETINGROOM ID="A2" NAME="A2" TIME="2004-06-25 15:30-17:30" PRICE="3000" PRESONAMOUNT="50" REMARK="2222222">
      <MEETINGITEM NAME="笔3" AMOUNT="30" PRICE="120.5" REMARK="测绘四测绘四测"/>
      <MEETINGITEM NAME="电脑2" AMOUNT="30" PRICE="120.5" REMARK="测绘四测绘四测"/>
    </MEETINGROOM>
    <MEETINGROOM ID="A3" NAME="A3" TIME="2004-06-25 15:30-17:30" PRICE="3000" PRESONAMOUNT="50" REMARK="3333333">
    </MEETINGROOM>
  </MEETINGROOMLIST>
</MEETINGROOMINFO>
对应的xslt如下:
<?xml version="1.0" encoding="GB2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="urn:schemas-microsoft-com:vml" version="1.0">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
      <div align="center">
        <FIELDSET>
        <LEGEND>会议室信息</LEGEND>
            <xsl:apply-templates select="MEETINGROOMINFO"/>
        </FIELDSET>
      </div>
    </xsl:template>

    <xsl:template match="MEETINGROOMLIST">
      <!--显示会议室信息-->
      <TABLE  backgcolor="silver" cellpadding="1" cellspacing="1" width="95%" bgcolor="silver" id="tblApplyList" align="center">
          <tr  bgcolor="#FFF4CA"  height = "20">
            <th width="15%" >会议室</th>
            <th width="30%" >时间</th>
            <th width="8%">价格</th>
            <th width="5%" >人数</th>
            <th width="34%" >备注</th>
            <th width="8%" ><a href="#" onclick="AddMeetingRoom()">增加</a></th>
          </tr>
          <xsl:apply-templates select="MEETINGROOM"/>
      </TABLE><p/>
      <!--显示每个会议室得的项目信息-->
      <xsl:for-each select="MEETINGROOM">
        <!--如果会议室下面没有项目,则不显示.-->
        <xsl:if test="count(./MEETINGITEM)>0">
        <table  backgcolor="silver" cellpadding="1" cellspacing="1" width="95%" bgcolor="silver" id="tblapplylist">
          <tr  bgcolor="#E1E1FF"  height = "22">
            <th style="text-align:left"  colspan="5">会议室:<xsl:value-of select="@NAME"/>   使用时间:<xsl:value-of select="@TIME"/>   单价:<xsl:value-of select="@PRICE"/>   人数:<xsl:value-of select="@PRESONAMOUNT"/>   备注:<xsl:value-of select="@REMARK"/></th>
          </tr>
          <tr  bgcolor="#FFF4CA"  height = "20">
            <th width="20%" >项目</th>
            <th width="10%" >数量</th>
            <th width="10%">单价</th>
            <th width="50%" >备注</th>
            <th width="8%" ><a href="#" onclick="alert({position()-1});return false;">新增</a></th>
          </tr>
          <xsl:apply-templates select="MEETINGITEM"/>
        </table><p/>
        </xsl:if>
      </xsl:for-each>
    </xsl:template>

    <xsl:template match="MEETINGROOM">
      <tr bgcolor="white">
          <td ><xsl:value-of select="@NAME"/></td>
          <td ><xsl:value-of select="@TIME"/></td>
          <td ><xsl:value-of select="@PRICE"/></td>
          <td ><xsl:value-of select="@PRESONAMOUNT"/></td>
          <td ><xsl:value-of select="@REMARK"/></td>
          <td ><a href="#" onclick="return DeleteMR({position()-1})">删除</a></td>
      </tr>
    </xsl:template>

    <xsl:template match="MEETINGITEM">
      <tr bgcolor="white">
        <td ><xsl:value-of select="@NAME"/></td>
        <td ><xsl:value-of select="@AMOUNT"/></td>
        <td ><xsl:value-of select="@PRICE"/></td>
        <td ><xsl:value-of select="@REMARK"/></td>
        <td ><a href="#" onclick="return DeleteMRItem({position()-1})">删除</a></td>
      </tr>
    </xsl:template>
</xsl:stylesheet>
现在问题是 MEETINGITEM 节点下的删除不能获得是对应那个 MEETINGROOM 的.不知有没有办法?

[ 本帖由 Eclipse 于 2004-7-9 11:15 最后编辑 ]

作者: Eclipse   发布时间: 2004-07-09

虽然和你的问题无直接的关系
但其中遍历的思想值得借鉴

xml+xslt中检索父ID

xml文件:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<root>
<a id="3">
<name>aaa</name>
<parentid>6</parentid>
</a>
<a id="1">
<name>bbb</name>
<parentid>3</parentid>
</a>
<a id="6">
<name>cc</name>
<parentid>0</parentid>
</a>
</root>

test.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<xsl:for-each select="//a">
<xsl:value-of select="name"/>
<xsl:variable name="path">
<xsl:call-template name="pathNode">
<xsl:with-param name="node" select="/root"/>
<xsl:with-param name="parentid" select="parentid"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="pathNode" select="msxml:node-set($path)"/>
<xsl:for-each select="$pathNode//parentid">
<xsl:sort select="@sortid" data-type="number" order="descending"/>
<xsl:value-of select="."/>==>
</xsl:for-each>
<xsl:value-of select="@id"/>
<br/>
</xsl:for-each>
</xsl:template>
<xsl:template name="pathNode">
<xsl:param name="node"/>
<xsl:param name="parentid">0</xsl:param>
<xsl:param name="sortid">0</xsl:param>

<parentid sortid="{$sortid}">
<xsl:choose>
<xsl:when test="$parentid != ''"><xsl:value-of select="$parentid"/></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>

</parentid>

<xsl:if test="number($parentid) >0">
<xsl:call-template name="pathNode">
<xsl:with-param name="node" select="$node/a[@id > number($parentid)]"/>
<xsl:with-param name="parentid" select="$node/a[@id = $parentid ]/parentid"/>
<xsl:with-param name="sortid" select="number($sortid) + 1"/>
</xsl:call-template>
</xsl:if>

</xsl:template>
</xsl:stylesheet>

[ 本帖由 zdzhuo 于 2004-7-10 13:56 最后编辑 ]

作者: zdzhuo   发布时间: 2004-07-10

position() 返回结点序号
last() 返回最后一个结点

position()=last()-1
不知道行不行 请试一下

作者: zdzhuo   发布时间: 2004-07-10

谢谢两位的回复.
现在能想到的一个折中方案是在xml节点中增加一个GUID 字段来唯一标识该节点,每次新增节点的时候维护这个根据当前GUID的值生成一个新的GUID并赋给新增节点,然后根据这个GUID来对节点进行修改或删除动作.
再次感谢两位的指教.:)

作者: Eclipse   发布时间: 2004-07-12

有那么麻烦吗?
对父节点可以通过".."来引用。

作者: 心不再流泪   发布时间: 2004-07-21