XML之XSL

作者: Tinyspot | 来源:发表于2022-07-13 07:00 被阅读0次

XSL

  • XML样式表,XSL Style Sheet,demo.xsl
  • 由 3 部分组成
    • XSLT - a language for transforming XML documents
    • XPath - a language for navigating in XML documents
    • XSL-FO - a language for formatting XML documents

XSLT

  • 可扩展样式表转换语言(Extensible Stylesheet Language Transformations)是一种样式转换标记语言,可以将 XML 数据档转换为另外的XML或其它格式,如HTML网页,纯文字
  • XML是载体,使用 XSLT 转换 XML 文档
  • 在线工具

2.1 语法说明

  • 根元素是 <xsl:stylesheet> 或 <xsl:transform>
  • match="/" 定义整个文档
  • <xsl:value-of select="element-path" /> 提取某个 XML 元素的值
  • <xsl:for-each>
  • <xsl:if test="expression">
  • 若直接取索引值,索引从 1 开始
<?xml version="1.0" encoding="utf-8"?>
<request>
    <users>
        <user>
            <name>tinyspot</name>
            <job>Java</job>
        </user>
        <user>
            <name>tinyspot</name>
            <job>Go</job>
        </user>
    </users>
</request>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="request">
        <request>
            <job><xsl:value-of select="users/user[2]/job" /></job>
            <employees>
                <xsl:for-each select="users/user">
                    <employee>
                        <alias><xsl:value-of select="name" /></alias>
                        <work><xsl:value-of select="job" /></work>
                    </employee>
                </xsl:for-each>
            </employees>
        </request>
    </xsl:template>
</xsl:stylesheet>

示例Demo

import org.dom4j.*
public static void main(String[] args) throws Exception {
    Document document = getRoleXml("demo.xml");
    // pattern 1:
    // URL resource = Thread.currentThread().getContextClassLoader().getResource("demo.xsl");
    // Document transDocument = transfor(document, new File(resource.getPath()));
    // pattern 2:
    Document transDocument = transfor(document, new File("src/main/resources/demo.xsl"));

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");

    XMLWriter xmlWriter = new XMLWriter(new FileWriter(new File("output.xml")));
    xmlWriter.write(transDocument);
    xmlWriter.close();
}

public static Document transfor(Document document, File file) throws TransformerException {
    DocumentResult result = new DocumentResult();
    DocumentSource source = new DocumentSource(document);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(file));
    transformer.transform(source, result);
    return result.getDocument();
}

public static Document getRoleXml(String xmlFile) throws DocumentException {
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xmlFile);
    SAXReader saxReader = new SAXReader();
    return saxReader.read(is);
}

字符串解析成Document

String xmlStr = "<user><name>tinyspot</name></user>";
// 将字符串转为XML
Document document = DocumentHelper.parseText(xmlStr);
Element rootElement = document.getRootElement();
String xslStr = "";
Document transDocument = transforStr(document, new ByteArrayInputStream(xslStr.getBytes("utf-8")));

public static Document transforStr(Document document, InputStream inputStream) throws TransformerException {
    Transformer transformer = factory.newTransformer(new StreamSource(inputStream));
}

相关文章

网友评论

      本文标题:XML之XSL

      本文链接:https://www.haomeiwen.com/subject/unnmbrtx.html