以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  痛苦,想知道在xsl中如何改变变量的值,是不是用with-param  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=15299)


--  作者:amlworkflow
--  发布时间:3/9/2005 5:17:00 PM

--  痛苦,想知道在xsl中如何改变变量的值,是不是用with-param
我现在需要在xsl中设置一个整型数,每一次循环都不一样,应该怎么做?是不是用para?清各位大虾指点一下。
--  作者:doubleG
--  发布时间:3/10/2005 9:04:00 AM

--  
<xsl:variable是一旦设定就不能改变的;<xsl:param是template接收参数的变量,<xsl:with-param是在<xsl:call-template时传参的参数,想要改变就只能用递归通过with-param和param来改变了。:)
--  作者:amlworkflow
--  发布时间:3/10/2005 4:40:00 PM

--  
非常感谢doubleG!
虽然with-param作为参数能改变,但只要一出作用调用就不存在了。我想在多次调用时,能够产生为一个唯一序列号,有办法吗?谢谢!
--  作者:doubleG
--  发布时间:3/10/2005 6:11:00 PM

--  
不太清楚你是用那个唯一ID做什么,但是在一个template内生成的变量作用域就仅限于该template,所以一般要做什么事情就在该template内做了,因为也没有返回值的这种说法。
--  作者:amlworkflow
--  发布时间:3/11/2005 9:05:00 AM

--  
我不知道能不能在同一个template中做
原XML文件如下;
<root>
 <a>
  <a1>
   <ID>a11</ID>
   <Name>A11</Name>
   <RID>3</RID>
  </a1>
  <a1>
   <ID>a12</ID>
   <Name>A12</Name>
   <RID>3</RID>
  </a1>
 </a>
 <b>
  <b1>
   <ID>b11</ID>
   <Name>B11</Name>
   <RID>3</RID>
  </b1>
  <b1>
   <ID>b12</ID>
   <Name>B12</Name>
   <RID>3</RID>
  </b1>
 </b>
 <r>
  <r1>
   <ID>r11</ID>
   <rs>a11</rs>
   <re>b12</re>
  </r1>
 </r>
</root>

目标XML文件如下:
<root>
 <a>
  <a1>
   <ID>1</ID>
   <Name>A11</Name>
   <RID>3</RID>
  </a1>
  <a1>
   <ID>2</ID>
   <Name>A12</Name>
   <RID>3</RID>
  </a1>
 </a>
 <b>
  <b1>
   <ID>3</ID>
   <Name>B11</Name>
   <RID>3</RID>
  </b1>
  <b1>
   <ID>4</ID>
   <Name>B12</Name>
   <RID>3</RID>
  </b1>
 </b>
 <r>
  <r1>
   <ID>5</ID>
   <rs>1</rs>
   <re>2</re>
  </r1>
  <r1>
   <ID>6</ID>
   <rs>2</rs>
   <re>3</re>
  </r1>  
 </r>
</root>
就是做如上的转换。
将所有字符串的ID号改成整型数的ID号,同时在关系(节点r)中插入相关节点。就是这样的。
有办法吗?



--  作者:doubleG
--  发布时间:3/11/2005 4:24:00 PM

--  
你的最终XML和目标XML不配套吧,第一个r1只有一个,第二个就有两个了,哈哈
代码都给你们写了,好歹我的代码也值几个钱吧。哎,最近穷得厉害啊,哪位在北京的有事没事请老哥吃顿饭啊,看我多辛苦啊。多省几顿饭钱啊,哈哈。

方法一:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:template match="root">
  <xsl:copy>
   <xsl:for-each select="child::*">
    <xsl:copy>
     <xsl:for-each select="child::*">
      <xsl:choose>
       <xsl:when test="name(..)!='r'"><xsl:call-template name="setCommonNode"/></xsl:when>
       <xsl:otherwise>
       <xsl:call-template name="setRefNode"/>
      </xsl:otherwise>
      </xsl:choose>
      
     </xsl:for-each>
    </xsl:copy>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 <xsl:template name="setCommonNode">
  <xsl:copy>
   <xsl:variable name="posValue" select="position()"/>
   <xsl:for-each select="child::*">
    <xsl:choose>
     <xsl:when test="name(.)='ID'">
      <xsl:element name="ID">
       <xsl:call-template name="setID">
        <xsl:with-param name="idtempValue" select="$posValue"/>
        <xsl:with-param name="curNode" select="../../preceding-sibling::*[1]"/>
       </xsl:call-template>
      </xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy-of select="."/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template name="setRefNode">
  <xsl:copy>
   <xsl:variable name="posValue" select="position()"/>
   <xsl:for-each select="child::*">
    <xsl:choose>
     <xsl:when test="name(.)='ID'">
      <xsl:element name="ID">
       
       <xsl:call-template name="setID">
        <xsl:with-param name="idtempValue" select="$posValue"/>
        <xsl:with-param name="curNode" select="../../preceding-sibling::*[1]"/>
       </xsl:call-template>
      </xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy>
      <xsl:call-template name="setRefID">
       <xsl:with-param name="refID" select="."/>
      </xsl:call-template>
      </xsl:copy>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 
 <xsl:template name="setRefID">
  <xsl:param name="refID"/>
  <xsl:variable name="refNode" select="../../..//ID[.=$refID]"/>
  <xsl:value-of select="count($refNode/preceding::ID) +1"/>
 </xsl:template>
 
 <xsl:template name="setID">
  <xsl:param name="idtempValue"/>
  <xsl:param name="curNode"/>
  <xsl:choose>
   <xsl:when test="count($curNode/preceding-sibling::*)!=0">
    <xsl:call-template name="setID">
     <xsl:with-param name="idtempValue" select="$idtempValue + count($curNode/child::*)"/>
     <xsl:with-param name="curNode" select="$curNode/preceding-sibling::*[1]"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$idtempValue + count($curNode/child::*)"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>


--  作者:doubleG
--  发布时间:3/11/2005 4:26:00 PM

--  
看着吓到了吧,其实那个方法笨,写着写着发现了。最近脑子不行了,要大补才行啊。赶紧请吃饭啊!!!!
方法二:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
 <xsl:template match="root">
  <xsl:copy>
   <xsl:for-each select="child::*">
    <xsl:copy>
     <xsl:for-each select="child::*">
      <xsl:choose>
       <xsl:when test="name(..)!='r'">
        <xsl:call-template name="setCommonNode"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:call-template name="setRefNode"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:for-each>
    </xsl:copy>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 <xsl:template name="setCommonNode">
  <xsl:copy>
   <xsl:variable name="posValue" select="position()"/>
   <xsl:for-each select="child::*">
    <xsl:choose>
     <xsl:when test="name(.)='ID'">
      <xsl:element name="ID">
       <xsl:value-of select="count(preceding::ID) +1"/>
      </xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy-of select="."/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 <xsl:template name="setRefNode">
  <xsl:copy>
   <xsl:variable name="posValue" select="position()"/>
   <xsl:for-each select="child::*">
    <xsl:choose>
     <xsl:when test="name(.)='ID'">
      <xsl:element name="ID">
       <xsl:value-of select="count(preceding::ID) +1"/>
      </xsl:element>
     </xsl:when>
     <xsl:otherwise>
      <xsl:copy>
       <xsl:call-template name="setRefID">
        <xsl:with-param name="refID" select="."/>
       </xsl:call-template>
      </xsl:copy>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </xsl:copy>
 </xsl:template>
 <xsl:template name="setRefID">
  <xsl:param name="refID"/>
  <xsl:variable name="refNode" select="../../..//ID[.=$refID]"/>
  <xsl:value-of select="count($refNode/preceding::ID) +1"/>
 </xsl:template>
</xsl:stylesheet>

好像也没少多少啊,嘿嘿


--  作者:amlworkflow
--  发布时间:3/11/2005 9:31:00 PM

--  
谢谢了,怎么给你报酬啊!顺便问一下:金钱,经验,文采有什么用途?


[此贴子已经被作者于2005-3-11 21:56:28编辑过]

--  作者:doubleG
--  发布时间:3/13/2005 1:20:00 PM

--  
我也不懂啊,要在北京的话请我吃饭吧,^_^
--  作者:amlworkflow
--  发布时间:3/13/2005 10:29:00 PM

--  
很想交你这个朋友,只可惜我不在北京,有机会去北京一定请吃饭。还有一个小问题请教一下:我的目标文件就是与原文件不一样,我想应该是可以添加的。你认为呢?


--  作者:清水眸子
--  发布时间:3/14/2005 3:26:00 PM

--  
好可怜。。。再帮我写几个,等我去了北京请你吃包子好了。。。
--  作者:doubleG
--  发布时间:3/14/2005 5:22:00 PM

--  
不要包子!!!成都小吃的包子已经吃得倒胃口了!!
--  作者:殷千炀
--  发布时间:3/28/2005 4:30:00 PM

--  
……我理解……穷人都靠这个过日子……哈哈
--  作者:ksai
--  发布时间:4/3/2005 9:29:00 AM

--  
一楼的楼主:
       我是一名C#\ XML的初学者,我发表自己的想法--------
                           你可以编一个C#程序,在XSLT中编写C#脚本(简称e好了),然后在
C#应用程序中向E传递参数(如Console.out),再用Transform()就可以了。
--  作者:amlworkflow
--  发布时间:4/7/2005 8:36:00 AM

--  
谢谢!我试试看.
--  作者:netet
--  发布时间:6/12/2005 2:37:00 PM

--  
关注
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
107.422ms