Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2534658
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2012-10-29 22:58:39

xslt文件xml2json.xslt:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="">
  3. <!--
  4.   Copyright (c) 2006, Doeke Zanstra
  5.   All rights reserved.

  6.   Redistribution and use in source and binary forms, with or without modification,
  7.   are permitted provided that the following conditions are met:

  8.   Redistributions of source code must retain the above copyright notice, this
  9.   list of conditions and the following disclaimer. Redistributions in binary
  10.   form must reproduce the above copyright notice, this list of conditions and the
  11.   following disclaimer in the documentation and/or other materials provided with
  12.   the distribution.

  13.   Neither the name of the dzLib nor the names of its contributors may be used to
  14.   endorse or promote products derived from this software without specific prior
  15.   written permission.

  16.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17.   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19.   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  20.   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21.   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22.   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23.   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  25.   THE POSSIBILITY OF SUCH DAMAGE.
  26. -->

  27.   <xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json"/>
  28.     <xsl:strip-space elements="*"/>
  29.   <!--contant-->
  30.   <xsl:variable name="d">0123456789</xsl:variable>

  31.   <!-- ignore document text -->
  32.   <xsl:template match="text()[preceding-sibling::node() or following-sibling::node()]"/>

  33.   <!-- string -->
  34.   <xsl:template match="text()">
  35.     <xsl:call-template name="escape-string">
  36.       <xsl:with-param name="s" select="."/>
  37.     </xsl:call-template>
  38.   </xsl:template>
  39.   
  40.   <!-- Main template for escaping strings; used by above template and for object-properties
  41.        Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
  42.   <xsl:template name="escape-string">
  43.     <xsl:param name="s"/>
  44.     <xsl:text>"
  45.     ">
  46.       " select="$s"/>
  47.     
  48.     "</xsl:text>
  49.   </xsl:template>
  50.   
  51.   <!-- Escape the backslash (\) before everything else. -->
  52.   <xsl:template name="escape-bs-string">
  53.     <xsl:param name="s"/>
  54.     <xsl:choose>
  55.       <xsl:when test="contains($s,'\')">
  56.         <xsl:call-template name="escape-quot-string">
  57.           <xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
  58.         </xsl:call-template>
  59.         <xsl:call-template name="escape-bs-string">
  60.           <xsl:with-param name="s" select="substring-after($s,'\')"/>
  61.         </xsl:call-template>
  62.       </xsl:when>
  63.       <xsl:otherwise>
  64.         <xsl:call-template name="escape-quot-string">
  65.           <xsl:with-param name="s" select="$s"/>
  66.         </xsl:call-template>
  67.       </xsl:otherwise>
  68.     </xsl:choose>
  69.   </xsl:template>
  70.   
  71.   <!-- Escape the double quote ("). -->
  72.   ">
  73.     "/>
  74.     
  75.       ($s,'"')">
  76.         ">
  77.           " select="concat(substring-before($s,'"'),'\"')"/>
  78.         
  79.         ">
  80.           " select="substring-after($s,'"')"/>
  81.         
  82.       
  83.       
  84.         ">
  85.           " select="$s"/>
  86.         
  87.       
  88.     
  89.   
  90.   
  91.   
  92.   ">
  93.     "/>
  94.     
  95.       
  96.       ($s,' ')">
  97.         ">
  98.           " select="concat(substring-before($s,' '),'\t',substring-after($s,' '))"/>
  99.         
  100.       
  101.       
  102.       ($s,' ')">
  103.         ">
  104.           " select="concat(substring-before($s,' '),'\n',substring-after($s,' '))"/>
  105.         
  106.       
  107.       
  108.       ($s,' ')">
  109.         ">
  110.           " select="concat(substring-before($s,' '),'\r',substring-after($s,' '))"/>
  111.         
  112.       
  113.       $s"/>
  114.     
  115.   

  116.   
  117.   ()[not(string(number())='NaN')]">
  118.     ."/>
  119.   

  120.   
  121.   ()[translate(.,'TRUE','true')='true']">true
  122.   ()[translate(.,'FALSE','false')='false']">false

  123.   
  124.   *[count(child::node())=0]">
  125.     ">
  126.       " select="local-name()"/>
  127.     
  128.     :null
  129.     *">,
  130.   

  131.   
  132.   *" name="base">
  133.     (preceding-sibling::*)">{
  134.     ">
  135.       " select="name()"/>
  136.     
  137.     :
  138.     ()"/>
  139.     *">,
  140.     (following-sibling::*)">}
  141.   

  142.   
  143.   *[count(../*[name(../*)=name(.)])=count(../*) and count(../*)&gt;1]">
  144.     (preceding-sibling::*)">[
  145.     
  146.       (child::node())">
  147.         null
  148.       
  149.       
  150.         ()"/>
  151.       
  152.     
  153.     *">,
  154.     (following-sibling::*)">]
  155.   
  156.   
  157.   
  158.   /">
  159.     ()
用于执行转换的java程序:

点击(此处)折叠或打开

  1. package com.mibridge.kingnod.util;

  2. import javax.xml.transform.Source;
  3. import javax.xml.transform.Transformer;
  4. import javax.xml.transform.TransformerConfigurationException;
  5. import javax.xml.transform.TransformerException;
  6. import javax.xml.transform.TransformerFactory;
  7. import javax.xml.transform.TransformerFactoryConfigurationError;
  8. import javax.xml.transform.stream.StreamSource;
  9. import javax.xml.transform.stream.StreamResult;
  10. import java.io.*;

  11. /**
  12.  * @author Henry Poter
  13.  * @version 1.0
  14.  * @since 2012-10-24
  15.  */
  16. public class Transform {

  17.     /**
  18.      * Performs an XSLT transformation, sending the results
  19.      * to System.out.
  20.      */
  21.     public static void main(String[] args) throws Exception {
  22.         if (args.length != 2) {
  23.             System.err.println(
  24.                 "Usage: java Transform [xmlfile] [xsltfile]");
  25.             System.exit(1);
  26.         }

  27.         File xmlFile = new File(args[0]);
  28.         File xsltFile = new File(args[1]);

  29.         OutputStream out = transform(xmlFile, xsltFile);
  30.         System.out.println(out.toString());
  31.     }

  32.     /**
  33.      * @param xmlFile
  34.      * @param xsltFile
  35.      * @return
  36.      * @throws TransformerFactoryConfigurationError
  37.      * @throws TransformerConfigurationException
  38.      * @throws TransformerException
  39.      */
  40.     private static OutputStream soap2xml(File xmlFile, File xsltFile)
  41.             throws TransformerFactoryConfigurationError,
  42.             TransformerConfigurationException, TransformerException {
  43.         // JAXP reads data using the Source interface
  44.         Source xmlSource = new StreamSource(xmlFile);
  45.         Source xsltSource = new StreamSource(xsltFile);

  46.         // the factory pattern supports different XSLT processors
  47.         TransformerFactory transFact =
  48.                 TransformerFactory.newInstance();
  49.         Transformer trans = transFact.newTransformer(xsltSource);
  50.         StringBuffer buffer = new StringBuffer();
  51.         OutputStream out = new ByteArrayOutputStream();
  52.        // trans.transform(xmlSource, new StreamResult(System.out));
  53.         trans.transform(xmlSource, new StreamResult(out));
  54.         return out;
  55.     }
  56.     /**
  57.      * xml文件通过xslt进行转换
  58.      * @param xmlFile 需要进行转换的xml文件
  59.      * @param xmlFile 需要进行转换的xml文件
  60.      * @return out 转换后的结果
  61.      */
  62.     private static OutputStream transform(File xmlFile, File xsltFile)
  63.             throws TransformerFactoryConfigurationError,
  64.             TransformerConfigurationException, TransformerException {
  65.         // JAXP reads data using the Source interface
  66.         Source xmlSource = new StreamSource(xmlFile);
  67.         Source xsltSource = new StreamSource(xsltFile);

  68.         // the factory pattern supports different XSLT processors
  69.         TransformerFactory transFact =
  70.                 TransformerFactory.newInstance();
  71.         Transformer trans = transFact.newTransformer(xsltSource);
  72.         StringBuffer buffer = new StringBuffer();
  73.         OutputStream out = new ByteArrayOutputStream();
  74.        // trans.transform(xmlSource, new StreamResult(System.out));
  75.         trans.transform(xmlSource, new StreamResult(out));
  76.         return out;
  77.     }
  78. }

阅读(3811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~