2011-03-29 2 views
8

Java에서 XSLT로 XML을 변환하려고합니다. 이를 위해 javax.xml.transform 패키지를 사용하고 있습니다. 그러나 예외는 javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet입니다. 이것은 내가 사용하고있는 코드입니다 :Java Transformer 오류 : 스타일 시트를 컴파일 할 수 없습니다.

public static String transform(String XML, String XSLTRule) throws TransformerException { 

    Source xmlInput = new StreamSource(XML); 
    Source xslInput = new StreamSource(XSLTRule); 

    TransformerFactory tFactory = TransformerFactory.newInstance(); 
    Transformer transformer = tFactory.newTransformer(xslInput); // this is the line that throws the exception 

    Result result = new StreamResult(); 
    transformer.transform(xmlInput, result); 

    return result.toString(); 
} 

나는 예외를 throw하는 선을 표시했습니다. 내가 방법을 입력하면

XSLTRule의 값은 이것이다 :

<xsl:stylesheet version='1.0' 
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
xmlns:msxsl='urn:schemas-microsoft-com:xslt' 
exclude-result-prefixes='msxsl' 
xmlns:ns='http://www.ibm.com/wsla'> 
    <xsl:strip-space elements='*'/> 
    <xsl:output method='xml' indent='yes'/> 
    <xsl:template match='@* | node()'> 
     <xsl:copy> 
      <xsl:apply-templates select='@* | node()'/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/ns:SLA 
          /ns:ServiceDefinition 
           /ns:WSDLSOAPOperation 
            /ns:SLAParameter[@name='Performance']"/> 
</xsl:stylesheet> 
+0

예외는 무엇입니까? –

+0

이것은'javax.xml.transform.TransformerConfigurationException'입니다. 나는 원래의 글을 편집했는데, 지금 거기에있다. – Ivan

+0

마지막 템플릿이 비어있는 이유는 무엇입니까? – Stephan

답변

9

constructor

public StreamSource(String systemId) 

가 URL로부터 StreamSource를 구축. XSLT의 내용을 대신 전달하는 것 같습니다. 이 시도 : 당신은 당신이 가지고있는 XSLT 문자열에서 스트림을 contruct하고 스트림으로 그것을 사용해야합니다

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
Result result = new StreamResult(baos); 
transformer.transform(xmlInput, result); 
return baos.toString(); 
+3

다른 방법으로'새로운 StreamSource (XSLTRule)'을'new StreamSource (new StringReader (XSLTRule))'로 변경하십시오. –

+0

고마워요! 하지만 난 여전히 'result result = new StreamResult();'에 문제가있다. 무엇을할까요? – Ivan

+0

@Ivan : 결과 결과 = 새 StreamResult();에서 어떤 문제가 있습니까? – MarcoS

1

:

File xslFile = new File("path/to/your/xslt"); 
TransformerFactory factory = TransformerFactory.newInstance(); 
Templates xsl = factory.newTemplates(new StreamSource(xslFile)); 

당신은 또한에 StreamResult 쓸 것이라는 OutputStream를 설정해야합니다 소스

InputStream xslStream = new ByteArrayInputStream(XSLTRule.getBytes("UTF-8")); 
Source xslInput = new StreamSource(xslStream); 

문자열로 결과를 얻으려면 :

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    Result result = new StreamResult(bos); 
    transformer.transform(xmlInput, result); 
    String s = new String(bos.toByteArray()); 
    System.out.println(s); 
+0

많은 감사합니다! 하지만 난 여전히 'result result = new StreamResult();'에 문제가있다. 무엇을할까요? – Ivan

+0

답변에 해당 코드를 추가했습니다. – Nishan

0

XSLTC를 사용하려면 클래스 경로에 xalan.jar (2.5), serializer.jar, xml-apis.jar 및 xercesImpl.jar을 넣으십시오.

+0

이 작업을 수행하지 마십시오. Xerces 프로젝트의 모든 패키지는 2010 년 이후에 포기되었습니다. 코드는 JVM에 저장됩니다. – Lambart

관련 문제