以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  [求助]xml数据转换时 控制类型长度问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=60501)


--  作者:benly
--  发布时间:3/27/2008 10:01:00 AM

--  [求助]xml数据转换时 控制类型长度问题


小弟不是专业搞XML的,对好多知识并不是太了解,因为经理着急要,没时间从头学习,所以直接像大家求助了,。。。原谅我的懒惰吧

我的问题是这样的,这个是我的一个XML,我需要对其中的ID值进行转换
<?xml version="1.0" encoding="gb2312"?>
<books>
 <book>
  <id>23</id>
  <name>三国</name>
 </book>
 <book>
  <id>123</id>
  <name>水浒</name>
 </book>
</books>
通过xsl转换,得到下面的xml,里面的ID值变成有规则的6位数,请问该怎么实现。。。
<?xml version="1.0" encoding="gb2312"?>
<books>
 <book>
  <id>B00023</id>
  <name>三国</name>
 </book>
 <book>
  <id>B00123</id>
  <name>水浒</name>
 </book>
</books>
我现在只了解一些基本的XSL知识,这个好像是XSLT可以实现的吧?希望大家给我指条路,先在这里谢过了,如果我说的不明白,大家给予指出,谢谢!


--  作者:benly
--  发布时间:3/28/2008 9:51:00 AM

--  
还是没人回复啊。。。小弟已经自己解决了,把xsl贴出来,希望能够帮助以后也有类似问题的朋友们


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <books>
            <xsl:apply-templates />
        </books>
    </xsl:template>
    <xsl:template match="//book">
        <book>
            <id><xsl:value-of select="concat(&quot;B&quot;,substring(concat(&quot;000000&quot;,id),string-length(concat(&quot;000000&quot;,id))-6,7))"/></id>
            <name><xsl:value-of select="name"/></name>
        </book>
    </xsl:template>
</xsl:stylesheet>


<!--  运行说明。。。
  id="12345678987654321"
  
  <xsl:value-of select="concat("B",substring(concat("000000",id),string-length(concat("000000",id))-6,7))"/>

  substring(concat("000000",id),string-length(concat("000000",id))-6,7)
  
  concat("000000",id)
  
  string-length(concat("000000",id))-6,7
  
  substring("abcdef12345678987654321",13,8)  //这个substring方法与JAVA里的Java.String.subString方法不一样, 先去掉前12位数,然后留下剩下的前8位。
    
  result =B78987654
-->


--  作者:Qr
--  发布时间:3/28/2008 3:11:00 PM

--  
这样写代码不更加容易理解吗?

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <books>
            <xsl:apply-templates />
        </books>
    </xsl:template>
    <xsl:template match="//book">
        <book>
            <id><xsl:value-of select="concat('B',substring('000000',1,6-string-length(id)),id)"/></id>
            <name><xsl:value-of select="name"/></name>
        </book>
    </xsl:template>
</xsl:stylesheet>


--  作者:gogy
--  发布时间:3/28/2008 11:59:00 PM

--  
试试<xsl:value-of select="format-number(12,'B00000')"/>
--  作者:Qr
--  发布时间:3/29/2008 11:51:00 AM

--  
以下是引用gogy在2008-3-28 23:59:00的发言:
试试<xsl:value-of select="format-number(12,'B00000')"/>



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