簡體   English   中英

如何使用Java讀取.xsl文件中的.properties文件?

[英]how to read a .properties file in a .xsl file using java?

情況 :我有以下樣式表/模板

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" x   
    mlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <head>
      <title>Hello</title>
    </head>
    <body >
      <div align="center">
        <table border="1">          
          <tr>
            <td >
              <h1 >
                <span>@HEADER@</span>
              </h1>
              <div>
                <p>
                    @DETAIL@
                </p>
              </div>              
          </tr>
        </table>
      </div>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我還有一個屬性文件,該屬性文件存儲鍵值對,如下所示:

DETAIL=This is format detail
HEADER=This is format header

問題 :我正在嘗試編寫一個Java程序以將xsl轉換為html文件,同時我想用.properties file提到的值替換.xsl指定的鍵值(@內)。我無法修改.xsl文件,我已經嘗試過TransformerFactory但是沒有用,有人可以幫助我實現這一目標嗎?

我想您想構建一個參數化XSL模板,其中參數位於屬性文件中。

XSL和屬性文件是否很大?

如果文件很小,則可以逐行讀取XSL文件並使用字符串替換:

// preparing parsed template
File fout = new File("parsed.xsl");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

// preparing map for properties
Map<String, String> map = new HashMap<>();
for (final String name: properties.stringPropertyNames())
    map.put(name, properties.getProperty(name));

// read file and replace key with value 
try (BufferedReader br = new BufferedReader(new FileReader(xslTemplate))) {
  String line;
  while ((line = br.readLine()) != null) {
     Iterator it = map.entrySet().iterator();
     while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        String temp = line.replace("@"+pair.getKey()+"@",pair.getValue());
        bw.write(temp);
        bw.newline();
  }
}

如果文件很大,則可以以其他方法使用此函數,該方法僅用於生成已解析的模板,然后在正常執行中使用新文件。

如果在Main.class中調用它,則可以添加一個參數,例如“ -c”,然后以編譯模式啟動該程序。

如果在Web應用程序中使用它,則可以在具有@Startup批注的單例中添加方法調用,並且當用戶更改XSL或屬性文件時也可以由用戶調用該方法調用

我沒有嘗試過代碼,也許有一些語法錯誤。 我希望解析中使用的邏輯很簡單。

代碼以根據您的語言環境讀取屬性文件。

<xsl:stylesheet version="2.0" xmlns:f="Functions"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- Code for reading proprty file -->
    <xsl:function name="f:getProperty" as="xs:string?">
        <xsl:param name="key" as="xs:string" />
        <xsl:variable name="lines" as="xs:string*"
            select="
                          for $x in 
                            for $i in tokenize($properties, '\n')[matches(., '^[^!#]')] return
                              tokenize($i, '=')
                            return translate(normalize-space($x), '\', '')" />
        <xsl:sequence select="$lines[index-of($lines, $key)+1]" />
    </xsl:function>

代碼以從屬性文件中打印消息。

<xsl:value-of select="f:getProperty('abc.xyz')" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM