今天写的一个xslt解析xml为wml
2006-08-24 12:37
375 查看
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/root">
<!-- Content Start -->
<wml>
<card id="show">
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
<p>
<br/>
<!--开始解析内容 -->
<xsl:apply-templates select="content" />
</p>
</card>
</wml>
<!-- Content End -->
</xsl:template>
<!--解析font标签 -->
<xsl:template match="font">
<xsl:element name="font">
<xsl:choose>
<xsl:when test="@size='large'">
<xsl:attribute name="size">4</xsl:attribute>
</xsl:when>
<xsl:when test="@size='normal'">
<xsl:attribute name="size">2</xsl:attribute>
</xsl:when>
<xsl:when test="@size='small'">
<xsl:attribute name="size">1</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:attribute name="color">
<xsl:value-of select="@color"></xsl:value-of>
</xsl:attribute>
<xsl:attribute name="align">
<xsl:value-of select="@align"></xsl:value-of>
</xsl:attribute>
<xsl:value-of select="."></xsl:value-of>
</xsl:element>
</xsl:template>
<!--解析a标签 -->
<xsl:template match="a">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="substring-before(@href,':')='http'">
<xsl:value-of select="@href"></xsl:value-of>
</xsl:when>
<xsl:when test="substring-before(@href,':')='tel'">
<!--拨打电话 -->
wtai://wp/mc;<xsl:value-of select="substring-after(@href,':')"></xsl:value-of>
</xsl:when>
<xsl:otherwise> http://<xsl:value-of select="@href"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."></xsl:value-of>
</xsl:element>
</xsl:template>
<!--解析p标签 -->
<xsl:template match="p">
<xsl:element name="p">
<xsl:attribute name="align">
<xsl:value-of select="@align"></xsl:value-of>
</xsl:attribute>
<xsl:apply-templates select="" />
</xsl:element>
</xsl:template>
<!--可扩展增加需要解析的标签 -->
</xsl:stylesheet>
部分代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/root">
<!-- Content Start -->
<wml>
<card id="show">
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
<p>
<br/>
<!--开始解析内容 -->
<xsl:apply-templates select="content" />
</p>
</card>
</wml>
<!-- Content End -->
</xsl:template>
<!--解析font标签 -->
<xsl:template match="font">
<xsl:element name="font">
<xsl:choose>
<xsl:when test="@size='large'">
<xsl:attribute name="size">4</xsl:attribute>
</xsl:when>
<xsl:when test="@size='normal'">
<xsl:attribute name="size">2</xsl:attribute>
</xsl:when>
<xsl:when test="@size='small'">
<xsl:attribute name="size">1</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:attribute name="color">
<xsl:value-of select="@color"></xsl:value-of>
</xsl:attribute>
<xsl:attribute name="align">
<xsl:value-of select="@align"></xsl:value-of>
</xsl:attribute>
<xsl:value-of select="."></xsl:value-of>
</xsl:element>
</xsl:template>
<!--解析a标签 -->
<xsl:template match="a">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="substring-before(@href,':')='http'">
<xsl:value-of select="@href"></xsl:value-of>
</xsl:when>
<xsl:when test="substring-before(@href,':')='tel'">
<!--拨打电话 -->
wtai://wp/mc;<xsl:value-of select="substring-after(@href,':')"></xsl:value-of>
</xsl:when>
<xsl:otherwise> http://<xsl:value-of select="@href"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."></xsl:value-of>
</xsl:element>
</xsl:template>
<!--解析p标签 -->
<xsl:template match="p">
<xsl:element name="p">
<xsl:attribute name="align">
<xsl:value-of select="@align"></xsl:value-of>
</xsl:attribute>
<xsl:apply-templates select="" />
</xsl:element>
</xsl:template>
<!--可扩展增加需要解析的标签 -->
</xsl:stylesheet>
部分代码
相关文章推荐
- 封装了一个用xslt样式将xml解析为xhtml的类TransformBinder(兼容FF和IE7.0)
- 今天做的一个ruby解析xml的小程序
- 今天找到一个好的DOM4J解析XML的方法elementByID。可以通过QName获取属性。记录下~
- 自己写一个好的PHP解析xml的类
- 在android开发中,经常用到去解析xml文件,常见的解析xml的方式有一下三种:SAX、Pull、Dom解析方式。最近做了一个android版的CSDN阅读器,用到了其中的两种(sax,pull)
- 今天在做rss解析的时候碰到一个奇怪的难题
- 关于一个xml时间解析造成的bug
- 用xslt解析xml
- xml解析2:使用递归解析给定的任意一个xml文档并且将其内容输出到命令行上
- XML解析工具一个
- 解析一个XML文件-Yahoo天气(xml文件有属性值)
- Java中Dom解析xml的一个实例
- javaWEB项目中使用XSLT解析生成的XML文件
- Rhyme/ 手写服务器 实现一个基于xml解析的简单的Tomcat服务器
- 一个模拟页面操作,解析xml输出,生成CSV文件的ruby程­序
- XML——XSLT的一个简单荔枝
- android如何使用DOM来解析XML+如果做一个表情的弹出框
- 【第三篇日记】C#解析XML文档和excel文档(弄GoogleEarth时遇到的一个问题,作为一个初学者就大补XML的内容)
- Javascript + Xslt 解析Xml为Html的方法