通过xsl解析xml(具体为TEI)生成html

本文档详细介绍了如何通过Java实现利用XML和XSL技术,将符合TEI标准的XML文档转换成HTML页面,以供展示和解析。

xml地址

https://www.oldbaileyonline.org/browse.jsp?foo=bar&path=sessionsPapers/17701024.xml&div=t17701024-22&xml=yes

TEI文档地址

https://tei-c.org/release/doc/tei-p5-exemplars/html/tei_lite.doc.html#U5-ptrs
https://tei-c.org/Vault/P4/doc/html/SA.html

使用xml和xsl生成html(java实现)

import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class xmlToHtmlByXsl {

    // 使用xml和xsl文件生成html文件
    public static String buildHtml(String xml, String xsl) throws Exception {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document xmlDoc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        Source xmlSource = new DOMSource(xmlDoc);
        
        StreamSource xslSource = new StreamSource(new ByteArrayInputStream(xsl.getBytes()));
        TransformerFactory tf = TransformerFactory.newInstance();
        Templates transformation = tf.newTemplates(xslSource);
        Transformer transformer = transformation.newTransformer();
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(bos);
        transformer.transform(xmlSource, result);

        return new String(bos.toByteArray(), StandardCharsets.UTF_8);
    }
    
    // 读取文件内容
    public static String readFileToString(File file) throws IOException {
        return IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8);
    }
    
    // 程序入口
    public static void main(String[] args) throws Exception {
        String xmlPath = "init.xml";
        String xslPath = "init.xsl";
        String htmlPath = "init.html";

        String xmlStr = readFileToString(new File(xmlPath));
        String xslStr = readFileToString(new File(xslPath));

        String html = buildHtml(xmlStr, xslStr);
        FileWriter fileWriter = new FileWriter(htmlPath);
        fileWriter.write(html);
        fileWriter.close();
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>

<!-- © 2003-2008 Old Bailey Proceedings Online -->
<TEI.2> 
  <text> 
    <body> 
      <div0 id="17701024" type="sessionsPaper" fragment="yes"> 
        <div1 type="trialAccount" id="t17701024-22"> 
          <xptr type="pageFacsimile" doc="177010240010"/>  
          <xptr type="preceedingDiv" divtype="trialAccount" id="t17701024-21"/>  
          <xptr type="followingDiv" divtype="trialAccount" id="t17701024-23"/>  
          <interp inst="t17701024-22" type="collection" value="BAILEY"/>  
          <interp inst="t17701024-22" type="year" value="1770"/>  
          <interp inst="t17701024-22" type="uri" value="sessionsPapers/17701024"/>  
          <interp inst="t17701024-22" type="date" value="17701024"/>  
          <join result="criminalCharge" id="t17701024-22-off100-c142" targOrder="Y" targets="t17701024-22-defend191 t17701024-22-off100 t17701024-22-verdict105"/>  
          <p>653. (M.) 
            <persName id="t17701024-22-defend191" type="defendantName">Robert Parker 
              <interp inst="t17701024-22-defend191" type="surname" value="Parker"/>  
              <interp inst="t17701024-22-defend191" type="given" value="Robert"/>  
              <interp inst="t17701024-22-defend191" type="gender" value="male"/> 
            </persName> was indicted for 
            <rs id="t17701024-22-off100" type="offenceDescription"> 
              <interp inst="t17701024-22-off100" type="offenceCategory" value="theft"/>  
              <interp inst="t17701024-22-off100" type="offenceSubcategory" value="grandLarceny"/> stealing a watch with the inside case pinchbeck, the outside case shagreen, value 5 l. and a pair of their shoe-buckles, value 10 s. a pair of silver knee buckles, value 5 s.
            </rs> the property of 
            <persName id="t17701024-22-victim193" type="victimName">William Barnet 
              <interp inst="t17701024-22-victim193" type="surname" value="Barnet"/>  
              <interp inst="t17701024-22-victim193" type="given" value="William"/>  
              <interp inst="t17701024-22-victim193" type="gender" value="male"/>  
              <join result="offenceVictim" targOrder="Y" targets="t17701024-22-off100 t17701024-22-victim193"/> 
            </persName> . 
            <rs id="t17701024-22-cd101" type="crimeDate">Sept. 11</rs>  
            <join result="offenceCrimeDate" targOrder="Y" targets="t17701024-22-off100 t17701024-22-cd101"/>. +
          </p>  
          <p> 
            <persName id="t17701024-22-person194">William Barnet 
              <interp inst="t17701024-22-person194" type="surname" value="Barnet"/>  
              <interp inst="t17701024-22-person194" type="given" value="William"/>  
              <interp inst="t17701024-22-person194" type="gender" value="male"/> 
            </persName> . I am 
            <rs id="t17701024-22-viclabel102" type="occupation">servant</rs>  
            <join result="persNameOccupation" targOrder="Y" targets="t17701024-22-victim193 t17701024-22-viclabel102"/> to Capt. Maynard.
          </p>  
          <p>Q. Have you been christened? *</p>  
          <p>* Barnet is 
            <rs id="t17701024-22-viclabel103" type="occupation">a black</rs>  
            <join result="persNameOccupation" targOrder="Y" targets="t17701024-22-victim193 t17701024-22-viclabel103"/> 
          </p>  
          <p>Barnet. Yes; I was christened in 1766. On the 11th of September I went to Shadwell Dock; as I was coming home I met with the prisoner; he asked me where I was going; I said to Holborn. He asked me to go into a public-house and drink with him, Two men came in; one of them pretended to be a Squire's son. One of them asked the way to High Holborn. The prisoner said he was going there, and would shew him the way. We went from there to 
            <placeName id="t17701024-22-crimeloc104">Holborn</placeName>  
            <interp inst="t17701024-22-crimeloc104" type="placeName" value="Holborn"/>  
            <interp inst="t17701024-22-crimeloc104" type="type" value="crimeLocation"/>  
            <join result="offencePlace" targOrder="Y" targets="t17701024-22-off100 t17701024-22-crimeloc104"/>; when we came to the Two Blue Posts he asked me to go in there and drink; I told him I would rather not; he prevailed upon me, and I went in with him. The 'Squire's son and another man came in there; the 'Squire's son proposed to toss up for some liquor; he said he would toss up for 50 l. The prisoner said he was a brewer's clerk, and he could not afford to toss up for so much money. They got tossing up for money. The prisoner asked to see my buckles; I took them out of my shoes; the prisoner laid them down upon his handkerchief. Soon after that the 'Squire's son said he wanted a watch for a girl of his. I took out my watch; the 'Squire's son asked me what I would take for it; I asked five guineas. The prisoner said let me alone. I will get you seven guineas for your watch and buckles. The prisoner and the 'Squire's son tossed up for 10 l. the prisoner lost, and borrowed some money to pay it; then he asked me to 
            <xptr type="pageFacsimile" doc="177010240011"/> go over the way with him, to borrow some more money. Almost as soon as I had crossed the pavement. I began to think my watch and buckles might not be safe; so I went back again, and they and my watch and buckles were gone. The 'Squire's son was to buy buckles, so he gave me his buckles to put in my shoes, which he said were silver, but I find they are not; I believe they are pewter.
          </p>  
          <p>Q. from the prisoner. Whether he did not go halves with me when I tossed up and them?</p>  
          <p>Barnet. No; I had no concern with the tossing up.</p>  
          <p>Prisoner's Defence.</p>  
          <p>He staked his watch and buckles against the 'Squire's son; he lost them, and he went out as I thought contented.</p>  
          <p>The Prisoner called Henry Street, who had known him seven years; 
            <persName id="t17701024-22-person195">Edward Perry 
              <interp inst="t17701024-22-person195" type="surname" value="Perry"/>  
              <interp inst="t17701024-22-person195" type="given" value="Edward"/>  
              <interp inst="t17701024-22-person195" type="gender" value="male"/> 
            </persName> one year; 
            <persName id="t17701024-22-person196">Joseph Yeoman 
              <interp inst="t17701024-22-person196" type="surname" value="Yeoman"/>  
              <interp inst="t17701024-22-person196" type="given" value="Joseph"/>  
              <interp inst="t17701024-22-person196" type="gender" value="male"/> 
            </persName> three years; 
            <persName id="t17701024-22-person197">Charles Powley 
              <interp inst="t17701024-22-person197" type="surname" value="Powley"/>  
              <interp inst="t17701024-22-person197" type="given" value="Charles"/>  
              <interp inst="t17701024-22-person197" type="gender" value="male"/> 
            </persName> and 
            <persName id="t17701024-22-person198">John Beale 
              <interp inst="t17701024-22-person198" type="surname" value="Beale"/>  
              <interp inst="t17701024-22-person198" type="given" value="John"/>  
              <interp inst="t17701024-22-person198" type="gender" value="male"/> 
            </persName> five years, who all gave him a good character.
          </p>  
          <p> 
            <rs id="t17701024-22-verdict105" type="verdictDescription"> 
              <interp inst="t17701024-22-verdict105" type="verdictCategory" value="guilty"/> Guilty
            </rs>. 
            <rs id="t17701024-22-punish106" type="punishmentDescription"> 
              <interp inst="t17701024-22-punish106" type="punishmentCategory" value="transport"/>  
              <join result="defendantPunishment" targOrder="Y" targets="t17701024-22-defend191 t17701024-22-punish106"/> T
            </rs>.
          </p> 
        </div1>
      </div0> 
    </body> 
  </text> 
</TEI.2>

xsl

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <table border="1">

          <!-- 表格头部 -->
          <tr bgcolor="#9acd32">
            <th align="left">id</th>
            <th align="left">role</th>
            <th align="left">surname</th>
            <th align="left">given</th>
            <th align="left">gender</th>
            <th align="left">occupation</th>
          </tr>

          <!-- 遍历所有的角色 -->
          <xsl:for-each select="/TEI.2/text/body/div0/div1/p/persName">
            <tr>
              <td><xsl:value-of select="@id"/></td>
              <td><xsl:value-of select="@type"/></td>
              <xsl:for-each select="interp">
                <td><xsl:value-of select="@value"/></td>
              </xsl:for-each>

              <!-- 开始查找occupation -->
              <td>

              <!-- 获取到当前的id并且保存到id变量中 -->
              <xsl:variable name="id" select="@id"/>

              <xsl:for-each select="/TEI.2/text/body/div0/div1/p/join">

                <xsl:if test="@result='persNameOccupation'">
                  <xsl:if test="contains(@targets, $id)">

                    <!-- 找到id对应的targets -->
                    <xsl:variable name="targets" select="@targets"/>

                    <xsl:for-each select="/TEI.2/text/body/div0/div1/p/rs">
                      <xsl:if test="@type='occupation'">

                        <!-- 根据targets找到对应的occupation -->
                        <xsl:if test="contains($targets, @id)">
                          <label><xsl:value-of select="current()"/></label>
                        </xsl:if>
                      </xsl:if>
                    </xsl:for-each>

                  </xsl:if>
                </xsl:if>
              </xsl:for-each>

              </td>
              <!-- 结束查找occupation -->

            </tr>
          </xsl:for-each>
        </table>

        <!-- 审判结果 -->
        <xsl:for-each select="/TEI.2/text/body/div0/div1/p/rs">
          <xsl:if test="@type = 'verdictDescription'">
            <label><xsl:value-of select="current()"/></label>
          </xsl:if>
          <xsl:if test="@type = 'punishmentDescription'">
            <labl><xsl:value-of select="current()"/></labl>
          </xsl:if>
        </xsl:for-each>

      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远喜欢薇尔莉特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值