[xslt]xml文件
[xslt]xml文件
----------------------------------------------
我如何在xslt中做控制,,,在新生成的xml中和原xml中的一样
(比如 我现在取的只能xsl:value-of取到123455,,而不能完整的取<field001>123455</field001>)
?
<?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"/>
<xsl:template match="/">
? <xsl:copy-of select="/" />
</xsl:template>
</xsl:stylesheet>
?
----------------------------------------------
在xsl中如何获取一个元素的子元素的名称?
?
例如:
<item>
?????????????????<http??........./>
?????????????????<email??........./>
?????????????????<ftp??........./>
</item>
?????事先我不知道 <item></item>中有哪些子元素,但是我要根据其出现的子元素的名称来做一些处理,比如,如果出现<http ../>子元素,我就给它加一个url属性;如果出现<email ../>子元素,我就给它加一个mailAddr 属性等等。。。。
?????现在就是要判断<item></item>中存在了几个子节点,每个子节点的名字是什么。。。。
????请问大家,如何获取子节点个数以及其名字啊???????
?
?
在xsl中可以用name()返回当前元素节点的名称;计算子节点个数可以用count(../item/*)得到。
?
----------------------------------------------
数据处理的时候,? 需要实现类似下面的一个处理:
有一个?? xml,?? 类似下面的结构
<root>
??? <rr?? name= "a ">
??????? <rrr> a1 </rrr>
??????? <rrr> a2 </rrr>
??????? <rrr> a3 </rrr>
??????? <rrr> a4 </rrr>
??? </rr>
??? <rr?? name= "b ">
??????? <rrr> b1 </rrr>
??????? <rrr> b2 </rrr>
??????? <rrr> b3 </rrr>
??????? <rrr> b4 </rrr>
??? </rr>
</root>
需要写一个?? xsl?? 文件,?? 将其显示为类似这样的效果:
-?? Libary:?? a
????? value:?? a1?? (/root/rr[1]/rrr[1])
????? value:?? a2?? (/root/rr[1]/rrr[2])
????? value:?? a3?? (/root/rr[1]/rrr[3])
????? value:?? a4?? (/root/rr[1]/rrr[4])
-?? Libary:?? b
????? value:?? b1?? (/root/rr[2]/rrr[1])
????? value:?? b2?? (/root/rr[2]/rrr[2])
????? value:?? b3?? (/root/rr[2]/rrr[3])
????? value:?? b4?? (/root/rr[2]/rrr[4])
对于显示成如上格式来说,?? 用?? xsl?? 可以很容易的实现,?? 但有如下两个需求不知道如何实现:
1.?? 对于?? Libary,?? 需要可以实现折叠的效果,?? 即默认情况下,?? 是不?? Value?? 数据的,?? 仅显示为:
+?? Libary:?? a
+?? Libary:?? b
只有单击?? +?? 号才显示出?? value?? 的数据
2.?? 对于每个?? value,?? 其后面的:???? (/root/rr[2]/rrr[1])??
这样的信息是该?? value?? 对应于?? xml?? 数据的?? xpath,?? 通过这个?? xpath,?? 可以唯一定位到这个?? value.???? 并且需要显示为?? value?? 的超链接接,?? 该超链接接的URL即为?? xpath(即显示的时间,???? value?? 项中,?? 括号部分的值是为显示的,?? 仅仅是?? value?? 各值上有一个超链接,?? 超链接的?? URL?? 地址为?? xpth(括号中显示的内容))
注:?? 因为需求的限制,?? 所以如果涉及到脚本,?? 则仅限?? java?? 脚本,?? 而且是嵌入在?? xsl?? 文件中的。
实际应用的?? xml?? 远比示例的复杂,?? 所以层次不是固定的?? /root/rr/rrr
最好能够直接根据当前解析到的结点推导出其 xpath。
?
?
?
<?xml?? version= "1.0 "?>
<xsl:stylesheet?? version= "1.0 "?? xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:output?? method= "html "?? encoding= "gb2312 "?? omit-xml-declaration= "yes "/>
<xsl:template?? match= "/ ">
<html>
<head>
<title> </title>
<script?? type= "text/javascript "?? language= "javascript ">
<![CDATA[
function?? btnOnClick(obj)?? {
if(obj.innerText== "+ ")?? {
theChild=obj.parentElement.nextSibling;
theChild.style.display= " ";
obj.innerText= "- ";
}
else?? {
theChild=obj.parentElement.nextSibling;
theChild.style.display= "none ";
obj.innerText= "+ ";
}
}
]]>
</script>
</head>
<body>
<xsl:apply-templates?? select= "root/* "?? />
</body>
</html>
</xsl:template>
<xsl:template?? match= "* ">
<xsl:param?? name= "parentXpath "> /root </xsl:param>
<xsl:variable?? name= "name "?? select= "name() "/>
<xsl:variable?? name= "currXpath "> <xsl:value-of?? select= "$parentXpath "/> / <xsl:value-of?? select= "name() "/> [ <xsl:value-of?? select= "count(preceding-sibling::*[name()=$name])+1 "/> ] </xsl:variable>
<div?? style= "margin:?? 3px?? 3px?? 6px?? 6px;
padding:?? 1px?? 1px?? 1px?? 5px;
vertical-align:?? top;
border-bottom:?? 1px?? outset?? #999999;
border-left:?? 1px?? dotted?? #999999; ">
<xsl:if?? test= "* ">
<div> <span?? id= " "?? onclick= "btnOnClick(this) "?? style= "cursor:?? pointer; "> + </span>?? Libary:?? <xsl:value-of?? select= "@name "/> </div>
<div?? style= "display:?? none; ">
<xsl:apply-templates>
<xsl:with-param?? name= "parentXpath "?? select= "$currXpath "/>
</xsl:apply-templates>
</div>
</xsl:if>
<xsl:if?? test= "not(*) ">
<div?? style= "margin-left:?? 5px ">
value:?? <xsl:value-of?? select= ". "/>?? ( <xsl:value-of?? select= "$currXpath "/> )
</div>
</xsl:if>
</div>
</xsl:template>
?
</xsl:stylesheet>
?
?
?
?
?
?