XSLT转换xml文件的案例
来源:互联网 发布:淘宝二手钢琴英昌u3 编辑:程序博客网 时间:2024/05/22 00:52
XML文件:
<?xml version="1.0" encoding="utf-8"?><doc><html><table a="b"></table><table style="a:b;" a="b"></table><table style="WIDTH:100px;" a="b"></table><table style="WIDTH:100PX;"><th TEXT-ALIGN='10' a="a"><td BACKGROUND-COLOR="red" TEXT-ALIGN='20' a='b'>1</td></th></table><span style="TEXT-DECORATION:underline"><a>ABC</a></span><span style="text-decoration:underline"><a>ABC</a></span><span style="TEXT-DECORATION:underline" a="b"><a>ABC</a></span><span style="color:#ff3300">dfafadssfas</span><span style="COLOR:#0033ff">dfafadssfas </span><span style="color:red"><a>EFG</a></span><p style="COLOR:#ff3300">dfafadssfas </p><p style="COLOR:#0033ff">dfafadssfas </p><p style="align:center">dfafadssfas </p><ul> <li> <p>1</p> <ul> <li> <p><span style="TEXT-DECORATION:underline"><a>ABC</a></span></p> <ul> <li> <p><span style="color:#ff3300"><span style="TEXT-DECORATION:underline"><a>ABC</a></span></span></p> </li> </ul> </li> </ul> </li> <li>1231</li></ul><ul><ul></ul></ul><strong a="1" b="2">a</strong><h1>a</h1><h2>q</h2><h3>w</h3><em>em</em></html></doc>
结果:
<?xml version="1.0" encoding="utf-8"?><jpxml> <table a="b" /> <table a="b" /> <table width="100px 			 								" a="b" /> <table width="100px 			 								"> <th align="10" a="a"> <td bgcolor="red" align="20" a="b">1</td> </th> </table> <u> <a>ABC</a> </u> <u> <a>ABC</a> </u> <u a="b"> <a>ABC</a> </u> <red>dfafadssfas</red> <blue>dfafadssfas </blue> <span> <a>EFG</a> </span> <red>dfafadssfas </red> <blue>dfafadssfas </blue> <p>dfafadssfas </p> <ul1> <li1> <p>1</p> <ul2> <li2> <p> <u> <a>ABC</a> </u> </p> <ul3> <li3> <p> <red> <u> <a>ABC</a> </u> </red> </p> </li3> </ul3> </li2> </ul2> </li1> <li1>1231</li1> </ul1> <ul1> <ul2 /> </ul1> <b a="1" b="2">a</b> <h1>a</h1> <h2>q</h2> <h3>w</h3> <i>em</i></jpxml>
XSL文件内容:
<?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" version="1.0" encoding="utf-8"indent="yes" cdata-section-elements="DataContent " /><xsl:template match="/"><jpxml><xsl:apply-templates /></jpxml></xsl:template><!-- ul,ol的转换 --><xsl:template match="ul|ol"><xsl:choose><xsl:when test="number(count(ancestor::ul)) > 0"><xsl:element name="{concat('ul',number(count(ancestor-or-self::ul)))}"><xsl:for-each select="@*"><xsl:copy/></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:when><xsl:otherwise><xsl:element name="ul1"><xsl:for-each select="@*"><xsl:copy/></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:otherwise></xsl:choose></xsl:template><!-- li的转换 --><xsl:template match="li"><xsl:choose><xsl:when test="number(count(ancestor::li)) > 0"><xsl:element name="{concat('li',number(count(ancestor-or-self::li)))}"><xsl:for-each select="@*"><xsl:copy/></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:when><xsl:otherwise><xsl:element name="li1"><xsl:for-each select="@*"><xsl:copy/></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:otherwise></xsl:choose></xsl:template><!-- 表格处理:style含有WIDTH的变成width属性,删除style属性 --><xsl:template match="table"><xsl:element name="{name(.)}"><xsl:choose><!-- 判断当前table是否含有style属性值WIDTH --><xsl:when test="contains(@style,'WIDTH')"><xsl:for-each select="@*"><!-- 判断当前属性是否是style --><xsl:choose><!-- 当前属性是style --><xsl:when test="name(.) = 'style'"><xsl:attribute name="width"><!--判断width的属性值是否有px --><xsl:choose><xsl:when test="contains(substring-before(substring-after(string(.),'WIDTH:'),';'),'px')"> <xsl:value-of select="number(substring-before(substring-after(string(.),'WIDTH:'),'px;'))"/>px </xsl:when> <xsl:when test="contains(substring-before(substring-after(string(.),'WIDTH:'),';'),'PX')"> <xsl:value-of select="number(substring-before(substring-after(string(.),'WIDTH:'),'PX;'))"/>px </xsl:when><!-- WITDTH值没有px --><xsl:otherwise><xsl:value-of select="number(substring-before(substring-after(string(.),'WIDTH:'),';'))"/></xsl:otherwise></xsl:choose></xsl:attribute></xsl:when><xsl:otherwise><!-- 当前属性不是style --><xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute></xsl:otherwise></xsl:choose></xsl:for-each></xsl:when><xsl:otherwise><xsl:for-each select="@*"><xsl:if test="name(.) != 'style'"><xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute></xsl:if></xsl:for-each></xsl:otherwise></xsl:choose><xsl:apply-templates/></xsl:element></xsl:template><!-- td处理:将TEXT-ALIGN属性改为align,BACKGROUND-COLOR改为bgcolor,属性值不变 --><xsl:template match="td"><xsl:element name="{name(.)}"><xsl:for-each select="@*"><xsl:choose><xsl:when test="name(.) = 'TEXT-ALIGN'"><xsl:attribute name="align"><xsl:value-of select="."/></xsl:attribute></xsl:when><xsl:when test="name(.) = 'BACKGROUND-COLOR'"><xsl:attribute name="bgcolor"><xsl:value-of select="."/></xsl:attribute></xsl:when><xsl:otherwise><xsl:copy/></xsl:otherwise></xsl:choose></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:template><!-- th处理:将TEXT-ALIGN属性改为align,属性值不变 --><xsl:template match="th"><xsl:element name="{name(.)}"><xsl:for-each select="@*"><xsl:choose><xsl:when test="name(.) = 'TEXT-ALIGN'"><xsl:attribute name="align"><xsl:value-of select="."/></xsl:attribute></xsl:when><xsl:otherwise><xsl:copy/></xsl:otherwise></xsl:choose></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:template><!-- <span style="TEXT-DECORATION:underline">ABC</span> --><!-- <span style="COLOR:#ff3300">dfafadssfas </span> --><!-- span标签下划线和两种颜色的转换和去除span标签中的属性和值 --><xsl:template match="span"><xsl:choose><xsl:when test="@style = 'TEXT-DECORATION:underline' or @style = 'text-decoration:underline'"><xsl:element name="u"><xsl:for-each select="@*"><xsl:if test="name(.) != 'style'"><xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute></xsl:if></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:when><xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'"><xsl:element name="red"><xsl:apply-templates/></xsl:element></xsl:when><xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'"><xsl:element name="blue"><xsl:apply-templates/></xsl:element></xsl:when><xsl:otherwise><xsl:element name="{name(.)}"><xsl:apply-templates/></xsl:element></xsl:otherwise></xsl:choose></xsl:template><!-- <p style="COLOR:#ff3300">dfafadssfas </p> --><!-- p标签两种颜色和删除属性和值的转换 --><xsl:template match="p"><xsl:choose><xsl:when test="@style = 'COLOR:#ff3300' or @style = 'color:#ff3300'"><xsl:element name="red"><xsl:apply-templates/></xsl:element></xsl:when><xsl:when test="@style = 'COLOR:#0033ff' or @style = 'color:#0033ff'"><xsl:element name="blue"><xsl:apply-templates/></xsl:element></xsl:when><xsl:otherwise><xsl:element name="{name(.)}"><xsl:apply-templates/></xsl:element></xsl:otherwise></xsl:choose></xsl:template><xsl:template match="a"><xsl:element name="{name(.)}"><xsl:for-each select="@*"><xsl:copy /></xsl:for-each><xsl:apply-templates /></xsl:element></xsl:template><xsl:template name="strong" match="strong"><xsl:element name="b"><xsl:for-each select="@*"><xsl:copy /></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:template><xsl:template match="em"><xsl:element name="i"><xsl:for-each select="@*"><xsl:copy /></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:template><xsl:template match="h1|h2|h3"><xsl:element name="{name(.)}"><xsl:for-each select="@*"><xsl:copy/></xsl:for-each><xsl:apply-templates/></xsl:element></xsl:template></xsl:stylesheet>
Java代码:
public static void main(String argv[]){String src = "E:\\workspace2\\test1\\WebContent\\test2\\a.xml";String dest = "E:\\workspace2\\test1\\WebContent\\test2\\a1.xml";String xslt = "E:\\workspace2\\test1\\WebContent\\test2\\a.xsl";//String dest = "E:\\workspace2\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\test1\\WEB-INF\\results\\a2.xml";//String xslt = "E:\\workspace2\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\test1\\WEB-INF\\templates\\a.xsl";File src2 = new File(src);File dest2 = new File(dest);File xslt2 = new File (xslt);Source srcSource = new StreamSource(src2);Result destResult = new StreamResult(dest2);Source xsltSource = new StreamSource(xslt2);try{TransformerFactory transFact = TransformerFactory.newInstance();Transformer trans = transFact.newTransformer(xsltSource);trans.transform(srcSource,destResult);}catch(TransformerConfigurationException e){e.printStackTrace();}catch(TransformerFactoryConfigurationError e){e.printStackTrace();}catch(TransformerException e){e.printStackTrace();}}
- XSLT转换xml文件的案例
- XML.转换XML的工具.XSLT
- XSLT转换XML时的中文问题
- XSLT转换XML生成网页的方式
- XSLT与XML的数据转换
- XSLT转换XML小结
- XSLT转换XML小结
- XSLT转换XML小结
- xslt转换xml
- XSLT转换XML实例
- xslt转换xml实例
- XSLT转换XML
- 通过XSLT转换XML
- xslt转换xml
- 在C#中用xslt文件格式化转换xml
- xslt格式化xml文件
- Javascript控制XSlt转换XML
- 【C#】XSLT转换XML实例
- 网页设计中的分割布局
- 【编译原理】How Compiler Works?
- 配置spring-security时报Configuration problem: Unable to locate Spring NamespaceHandler for XML schema
- High Dynamic Range Imaging
- [LeetCode] Add Binary
- XSLT转换xml文件的案例
- Android加载APK中的资源文件
- 让source insight支持AT&T汇编语法高亮
- string头文件与cstring头文件
- LINUX下安装ORACLE时SWAP不足的两种解决方法
- 我的第一个JAVA程序之计算器
- 实例教程:1小时学会Python
- POJ 1128 食物链
- POJ 1611 The Suspects