2016-06-04 6 views
0

상황는 다음과 같이 나는 또한 속성을 가지고java를 사용하여 .xsl 파일에서 .properties 파일을 읽는 방법?</p> <pre><code>DETAIL=This is format detail HEADER=This is format header </code></pre> <p><strong>문제</strong>이 : 내가하려고

<?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>@[email protected]</span> 
       </h1> 
       <div> 
       <p> 
        @[email protected] 
       </p> 
       </div>    
      </tr> 
     </table> 
     </div> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

는 저장 키 - 값 쌍을 다음과 같은 스타일/템플릿을 제기했다 xslhtml 파일로 변환하기 위해 자바 프로그램을 작성하십시오. 그래서 나는 .xsl (@로 묶인) 키의 값을 언급 된 값으로 대체하려고합니다 .properties file에서. 내 제약 조건은 .xsl 파일을 수정할 수 없다. TransformerFactory을 시도했지만 아무도이 문제를 해결할 수 없습니까?

+1

당신은 위의 XSLT 문서를 처리하는 XSLT (2.0)를 사용하고 글로벌 매개 변수'로 참조'@은'@ PROPERTY_NAME 중 하나를 대체 할 수있는 '와'< xsl : value-of select = "$ PROPERTY_NAME"/>'이면 새 XSLT를 실행하고 속성 파일에있는 모든 속성을 매개 변수로 변환에 전달할 수 있습니다. 그러나 질문 샘플에서 이름은 일치하지 않습니다 (예 :'@ HEADER @ '대'FORMAT_HEADER'). –

답변

0

매개 변수가 속성 파일에있는 매개 변수식 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"와 같은 인수를 추가하고 컴파일 모드에서 프로그램을 시작할 수 있습니다. 당신은 웹 응용 프로그램에서 사용하는 경우 그가 XSL 또는 등록 정보 파일을 변경할 때

, 당신은

내가하지 않은 사용자에 의해 @Startup 주석과 싱글에서 메서드 호출을 추가하고라고도 할 수 있습니다 tryed the code, 어쩌면 구문 오류가 있습니다. 구문 분석에 사용 된 논리가 간단하기를 바랍니다.

0

로캘에 따라 읽기 속성 파일에 코드를 추가하십시오.

<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')" />