以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  [分享]发一个我写的通用xsl  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=41130)


--  作者:hexun831012
--  发布时间:12/12/2006 3:25:00 PM

--  [分享]发一个我写的通用xsl
xsl:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!--Copyright(C) 2003-2006 Hexsoft, All Right Reserved.-->
 <xsl:template match="/">
  <div class="xml">
   <span class="xmlPI">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
   <br />
   <xsl:apply-templates />
  </div>
 </xsl:template>
 <!--Processing Instruction Template-->
 <xsl:template match="processing-instruction()">
  <span class="xmlPI">
   <xsl:text>&lt;?</xsl:text>
   <xsl:value-of select="name()" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="." />
   <xsl:text>?&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Comment Template-->
 <xsl:template match="comment()">
  <span class="xmlComment">
   <xsl:text>&lt;!--</xsl:text>
   <xsl:value-of select="." />
   <xsl:text>--&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Attribute Template-->
 <xsl:template match="@*">
  <xsl:text> </xsl:text>
  <span class="xmlAttribute">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>="</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <xsl:text>"</xsl:text>
 </xsl:template>
 <!--Text Template-->
 <xsl:template match="text()">
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <br />
 </xsl:template>
 <!--Empty Element Template-->
 <xsl:template match="*">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text> /&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Text Element Template-->
 <xsl:template match="*[text()]">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text>&gt;</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="text()" />
  </span>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Parent Element Template-->
 <xsl:template match="*[*]">
  <a onclick="collapseXml()">
   <img src="image/expand.png" />
   <xsl:text>&lt;</xsl:text>
   <span class="xmlName">
    <xsl:value-of select="name()" />
   </span>
   <xsl:apply-templates select="@*" />
   <xsl:text>&gt;</xsl:text>
  </a>
  <div>
   <xsl:apply-templates />
  </div>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
</xsl:stylesheet>
css:
/* Xml Class */
.xml
{
 color: blue;
 padding-left: 12px;
}
.xml a
{
 cursor: pointer;
 margin-left: -11px;
}
.xml div
{
 margin-left: 12px;
}
.xmlAttribute
{
 color: red;
}
.xmlComment
{
 color: green;
}
.xmlName
{
 color: maroon;
}
.xmlPI
{
 color: gray;
}
.xmlText
{
 color: black;
}
javascript:
/* Collapse Xml */
function collapseXml()
{
 var a = event.srcElement;
 if(a.nodeName != "A")
 {
  a = a.parentNode;
 }
 var div = a.nextSibling;
 if(div.style.display == "")
 {
  div.style.display = "none";
  a.firstChild.src = "image/collapse.png";
 }
 else
 {
  div.style.display = "";
  a.firstChild.src = "image/expand.png";
 }
}
--  作者:hexun831012
--  发布时间:12/12/2006 3:27:00 PM

--  
申请精华
申请至顶
--  作者:hexun831012
--  发布时间:12/14/2006 12:19:00 PM

--  
应admin要求,扩充并重发一遍
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="xml.xsl" type="text/xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <!--Copyright(C) 2003-2006 Hexsoft, All Right Reserved.-->
 <xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>
     <xsl:value-of select="//comment()" />
    </title>
    <style type="text/css">
     .xml
     {
      font: normal normal normal 9pt Tahoma;
      color: blue;
     }
     .xml a
     {
      cursor: pointer;
     }
     .xml div
     {
      margin-left: 12px;
     }
     .xmlAttribute
     {
      color: red;
     }
      .xmlComment
     {
      color: green;
     }
      .xmlName
     {
      color: maroon;
     }
      .xmlPI
     {
      color: gray;
     }
      .xmlText
     {
      color: black;
     }
    </style>
    <script type="text/javascript">
     function collapseXml()
     {
      var a = event.srcElement;
      if(a.nodeName != "A")
      {
       a = a.parentNode;
      }
      var div = a.nextSibling;
      if(div.style.display == "")
      {
       div.style.display = "none";
      }
      else
      {
       div.style.display = "";
      }
     }
    </script>
   </head>
   <body class="xml">
    <span class="xmlPI">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
    <br />
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>
 <!--Processing Instruction Template-->
 <xsl:template match="processing-instruction()">
  <span class="xmlPI">
   <xsl:text>&lt;?</xsl:text>
   <xsl:value-of select="name()" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="." />
   <xsl:text>?&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Comment Template-->
 <xsl:template match="comment()">
  <span class="xmlComment">
   <xsl:text>&lt;!--</xsl:text>
   <xsl:value-of select="." />
   <xsl:text>--&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Attribute Template-->
 <xsl:template match="@*">
  <xsl:text> </xsl:text>
  <span class="xmlAttribute">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>="</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <xsl:text>"</xsl:text>
 </xsl:template>
 <!--Text Template-->
 <xsl:template match="text()">
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <br />
 </xsl:template>
 <!--Empty Element Template-->
 <xsl:template match="*">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text> /&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Text Element Template-->
 <xsl:template match="*[text()]">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text>&gt;</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="text()" />
  </span>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Parent Element Template-->
 <xsl:template match="*[*]">
  <a onclick="collapseXml()">
   <xsl:text>&lt;</xsl:text>
   <span class="xmlName">
    <xsl:value-of select="name()" />
   </span>
   <xsl:apply-templates select="@*" />
   <xsl:text>&gt;</xsl:text>
  </a>
  <div>
   <xsl:apply-templates />
  </div>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
</xsl:stylesheet>
--  作者:hexun831012
--  发布时间:3/5/2007 11:35:00 AM

--  
发现一点问题,重新修订过了
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright&copy; 2003-2006 Hexsoft, All Right Reserved.-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>
 <!--Root Template-->
 <xsl:template match="/">
  <html version="1.1" xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>
     <xsl:value-of select="comment()"/>
    </title>
    <style type="text/css">.xml{font:normal normal normal 9pt Tahoma;color:blue}.xml a{cursor:pointer}.xml div{margin-left:12px}.xmlAttribute{color:red}.xmlComment{color:green}.xmlName{color:maroon}.xmlPI{color:gray}.xmlText{color:black}</style>
    <script type="text/javascript">function collapseXml(){var a=event.srcElement;if(a.nodeName!="A"){a=a.parentNode;}var div=a.nextSibling;if(div.style.display==""){div.style.display="none";}else{div.style.display="";}}</script>
   </head>
   <body class="xml">
    <span class="xmlPI">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
    <br/>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>
 <!--Processing Instruction Template-->
 <xsl:template match="processing-instruction()">
  <span class="xmlPI">
   <xsl:text>&lt;?</xsl:text>
   <xsl:value-of select="name()"/>
   <xsl:text></xsl:text>
   <xsl:value-of select="."/>
   <xsl:text>?&gt;</xsl:text>
  </span>
  <br/>
 </xsl:template>
 <!--Comment Template-->
 <xsl:template match="comment()">
  <span class="xmlComment">
   <xsl:text>&lt;!--</xsl:text>
   <xsl:value-of select="."/>
   <xsl:text>--&gt;</xsl:text>
  </span>
  <br/>
 </xsl:template>
 <!--Text Template-->
 <xsl:template match="text()">
  <span class="xmlText">
   <xsl:value-of select="."/>
  </span>
  <br/>
 </xsl:template>
 <!--Attribute Template-->
 <xsl:template match="@*">
  <xsl:text> </xsl:text>
  <span class="xmlAttribute">
   <xsl:value-of select="name()"/>
  </span>
  <xsl:text>="</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="."/>
  </span>
  <xsl:text>"</xsl:text>
 </xsl:template>
 <!--Empty Element Template-->
 <xsl:template match="*">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()"/>
  </span>
  <xsl:apply-templates select="@*"/>
  <xsl:text> /&gt;</xsl:text>
  <br/>
 </xsl:template>
 <!--Text Element Template-->
 <xsl:template match="*[text()]">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()"/>
  </span>
  <xsl:apply-templates select="@*"/>
  <xsl:text>&gt;</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="."/>
  </span>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()"/>
  </span>
  <xsl:text>&gt;</xsl:text>
  <br/>
 </xsl:template>
 <!--Parent Element Template-->
 <xsl:template match="*[node() and not(text())]">
  <a onclick="collapseXml()">
   <xsl:text>&lt;</xsl:text>
   <span class="xmlName">
    <xsl:value-of select="name()"/>
   </span>
   <xsl:apply-templates select="@*"/>
   <xsl:text>&gt;</xsl:text>
  </a>
  <div>
   <xsl:apply-templates/>
  </div>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()"/>
  </span>
  <xsl:text>&gt;</xsl:text>
  <br/>
 </xsl:template>
</xsl:stylesheet>
--  作者:alittlecat
--  发布时间:3/5/2007 11:53:00 AM

--  
各位高手,我想知道一些关于xml在vxworks下用c语言解析的方法,或是原代码也好.
请个位高手帮忙!!!现在急用.
--  作者:hexun831012
--  发布时间:3/6/2007 9:16:00 AM

--  
用高级语言解析xml需要用到循环递归,就以根节点为入口,循环解析每个节点所有可能的自节点的算法,代码我就不写了,要视不同的文档定义而言,虽然有通用的方法,但效率低下,不推荐
--  作者:hexun831012
--  发布时间:4/25/2007 5:09:00 PM

--  
重发通用XSL,支持firefox啦!!!
<?xml version="1.0" encoding="utf-8"?>
<!--Copyright&copy; 2003-2007 Hexsoft.org, All Right Reserved.-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" indent="no" encoding="utf-8" />
 <!--HTML Template-->
 <xsl:template match="/">
  <html version="1.1" xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>
     <xsl:value-of select="comment()" />
    </title>
    <style type="text/css">
.xml
{
 font-family: Tahoma;
 font-size: 9pt;
 color: blue;
}
.xml div
{
 margin-left: 16px;
}
.xmlAttribute
{
 color: red;
}
.xmlComment
{
 color: green;
}
.xmlName
{
 color: maroon;
}
.xmlPI
{
 color: gray;
}
.xmlText
{
 color: black;
}
    </style>
    <script type="text/javascript">
function collapse(a)
{
 var div = a.nextSibling;
 if(div.style.display == "")
 {
  div.style.display = "none";
 }
 else
 {
  div.style.display = "";
 }
}
    </script>
   </head>
   <body class="xml">
    <span class="xmlPI">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
    <br />
    <xsl:apply-templates />
   </body>
  </html>
 </xsl:template>
 <!--Attribute Template-->
 <xsl:template match="@*">
  <xsl:text> </xsl:text>
  <span class="xmlAttribute">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>="</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <xsl:text>"</xsl:text>
 </xsl:template>
 <!--Processing Instruction Template-->
 <xsl:template match="processing-instruction()">
  <span class="xmlPI">
   <xsl:text>&lt;?</xsl:text>
   <xsl:value-of select="name()" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="." />
   <xsl:text>?&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Comment Template-->
 <xsl:template match="comment()">
  <span class="xmlComment">
   <xsl:text>&lt;!-- </xsl:text>
   <xsl:value-of select="." />
   <xsl:text> --&gt;</xsl:text>
  </span>
  <br />
 </xsl:template>
 <!--Text Template-->
 <xsl:template match="text()">
  <xsl:if test="text() != ''">
   <span class="xmlText">
    <xsl:value-of select="." />
   </span>
   <br />
  </xsl:if>
 </xsl:template>
 <!--Empty Element Template-->
 <xsl:template match="*">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text> /&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Text Element Template-->
 <xsl:template match="*[text()]">
  <xsl:text>&lt;</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:apply-templates select="@*" />
  <xsl:text>&gt;</xsl:text>
  <span class="xmlText">
   <xsl:value-of select="." />
  </span>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
 <!--Node Element Template-->
 <xsl:template match="*[*]">
  <a onclick="collapse(this)">
   <xsl:text>&lt;</xsl:text>
   <span class="xmlName">
    <xsl:value-of select="name()" />
   </span>
   <xsl:apply-templates select="@*" />
   <xsl:text>&gt;</xsl:text>
  </a>
  <div>
   <xsl:apply-templates />
  </div>
  <xsl:text>&lt;/</xsl:text>
  <span class="xmlName">
   <xsl:value-of select="name()" />
  </span>
  <xsl:text>&gt;</xsl:text>
  <br />
 </xsl:template>
</xsl:stylesheet>
--  作者:火鸟
--  发布时间:7/7/2007 5:49:00 PM

--  
顶。
--  作者:insky
--  发布时间:7/12/2007 10:50:00 PM

--  
再顶。。。
--  作者:zhu_ruixian
--  发布时间:7/16/2007 8:12:00 PM

--  
好牛的xsl!
--  作者:Qr
--  发布时间:7/18/2007 1:29:00 PM

--  
前些天也用js+MSXML 方式遍历DOM节点,仿IE浏览XML方式输出。改天整理贴上来。
--  作者:火鸟
--  发布时间:7/19/2007 9:28:00 AM

--  
hexun831012你的通用xslt在MSXML下面也许是对的,可在Saxon下面通不过检查,有很多二义性。
--  作者:hexun831012
--  发布时间:7/19/2007 8:25:00 PM

--  
我的通用XSL是针对浏览器的,在IE和MF的解析器下都是完全一样的效果,他们都是完全符合W3C的标准的
--  作者:KELLY5677
--  发布时间:7/30/2007 4:14:00 PM

--  
你放了不少血了。。

--  作者:fifastar
--  发布时间:9/5/2007 5:59:00 PM

--  
这个是否就是显示全部结点的通用xsl呢?
--  作者:redskywy
--  发布时间:9/6/2007 9:35:00 PM

--  
问一下, 为什么叫通用 xsl
通用在哪里了?
--  作者:无忧的大脑门
--  发布时间:3/17/2008 2:54:00 PM

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