xslt文件xml2json.xslt:
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0" xmlns:xsl="">
- <!--
- Copyright (c) 2006, Doeke Zanstra
- All rights reserved.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer. Redistributions in binary
- form must reproduce the above copyright notice, this list of conditions and the
- following disclaimer in the documentation and/or other materials provided with
- the distribution.
- Neither the name of the dzLib nor the names of its contributors may be used to
- endorse or promote products derived from this software without specific prior
- written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- THE POSSIBILITY OF SUCH DAMAGE.
- -->
- <xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json"/>
- <xsl:strip-space elements="*"/>
- <!--contant-->
- <xsl:variable name="d">0123456789</xsl:variable>
- <!-- ignore document text -->
- <xsl:template match="text()[preceding-sibling::node() or following-sibling::node()]"/>
- <!-- string -->
- <xsl:template match="text()">
- <xsl:call-template name="escape-string">
- <xsl:with-param name="s" select="."/>
- </xsl:call-template>
- </xsl:template>
-
- <!-- Main template for escaping strings; used by above template and for object-properties
- Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
- <xsl:template name="escape-string">
- <xsl:param name="s"/>
- <xsl:text>"
- ">
- " select="$s"/>
-
- "</xsl:text>
- </xsl:template>
-
- <!-- Escape the backslash (\) before everything else. -->
- <xsl:template name="escape-bs-string">
- <xsl:param name="s"/>
- <xsl:choose>
- <xsl:when test="contains($s,'\')">
- <xsl:call-template name="escape-quot-string">
- <xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
- </xsl:call-template>
- <xsl:call-template name="escape-bs-string">
- <xsl:with-param name="s" select="substring-after($s,'\')"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:call-template name="escape-quot-string">
- <xsl:with-param name="s" select="$s"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- Escape the double quote ("). -->
- ">
- "/>
-
- ($s,'"')">
- ">
- " select="concat(substring-before($s,'"'),'\"')"/>
-
- ">
- " select="substring-after($s,'"')"/>
-
-
-
- ">
- " select="$s"/>
-
-
-
-
-
-
- ">
- "/>
-
-
- ($s,' ')">
- ">
- " select="concat(substring-before($s,' '),'\t',substring-after($s,' '))"/>
-
-
-
- ($s,'
')">
- ">
- " select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/>
-
-
-
- ($s,'
')">
- ">
- " select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/>
-
-
- $s"/>
-
-
-
- ()[not(string(number())='NaN')]">
- ."/>
-
-
- ()[translate(.,'TRUE','true')='true']">true
- ()[translate(.,'FALSE','false')='false']">false
-
- *[count(child::node())=0]">
- ">
- " select="local-name()"/>
-
- :null
- *">,
-
-
- *" name="base">
- (preceding-sibling::*)">{
- ">
- " select="name()"/>
-
- :
- ()"/>
- *">,
- (following-sibling::*)">}
-
-
- *[count(../*[name(../*)=name(.)])=count(../*) and count(../*)>1]">
- (preceding-sibling::*)">[
-
- (child::node())">
- null
-
-
- ()"/>
-
-
- *">,
- (following-sibling::*)">]
-
-
-
- /">
- ()
用于执行转换的java程序:
- package com.mibridge.kingnod.util;
- import javax.xml.transform.Source;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.TransformerFactoryConfigurationError;
- import javax.xml.transform.stream.StreamSource;
- import javax.xml.transform.stream.StreamResult;
- import java.io.*;
- /**
- * @author Henry Poter
- * @version 1.0
- * @since 2012-10-24
- */
- public class Transform {
- /**
- * Performs an XSLT transformation, sending the results
- * to System.out.
- */
- public static void main(String[] args) throws Exception {
- if (args.length != 2) {
- System.err.println(
- "Usage: java Transform [xmlfile] [xsltfile]");
- System.exit(1);
- }
- File xmlFile = new File(args[0]);
- File xsltFile = new File(args[1]);
- OutputStream out = transform(xmlFile, xsltFile);
- System.out.println(out.toString());
- }
- /**
- * @param xmlFile
- * @param xsltFile
- * @return
- * @throws TransformerFactoryConfigurationError
- * @throws TransformerConfigurationException
- * @throws TransformerException
- */
- private static OutputStream soap2xml(File xmlFile, File xsltFile)
- throws TransformerFactoryConfigurationError,
- TransformerConfigurationException, TransformerException {
- // JAXP reads data using the Source interface
- Source xmlSource = new StreamSource(xmlFile);
- Source xsltSource = new StreamSource(xsltFile);
- // the factory pattern supports different XSLT processors
- TransformerFactory transFact =
- TransformerFactory.newInstance();
- Transformer trans = transFact.newTransformer(xsltSource);
- StringBuffer buffer = new StringBuffer();
- OutputStream out = new ByteArrayOutputStream();
- // trans.transform(xmlSource, new StreamResult(System.out));
- trans.transform(xmlSource, new StreamResult(out));
- return out;
- }
- /**
- * xml文件通过xslt进行转换
- * @param xmlFile 需要进行转换的xml文件
- * @param xmlFile 需要进行转换的xml文件
- * @return out 转换后的结果
- */
- private static OutputStream transform(File xmlFile, File xsltFile)
- throws TransformerFactoryConfigurationError,
- TransformerConfigurationException, TransformerException {
- // JAXP reads data using the Source interface
- Source xmlSource = new StreamSource(xmlFile);
- Source xsltSource = new StreamSource(xsltFile);
- // the factory pattern supports different XSLT processors
- TransformerFactory transFact =
- TransformerFactory.newInstance();
- Transformer trans = transFact.newTransformer(xsltSource);
- StringBuffer buffer = new StringBuffer();
- OutputStream out = new ByteArrayOutputStream();
- // trans.transform(xmlSource, new StreamResult(System.out));
- trans.transform(xmlSource, new StreamResult(out));
- return out;
- }
- }
阅读(3856) | 评论(0) | 转发(0) |