2011-11-05 2 views
2

XML 파일 -XSLT 및 Java를 사용하여 XML 노드 텍스트 바꾸기?

<Remarks> 
<Remark> 
<Cid>2009-1</Cid> 
<Date>3-11-2011 2:55:0</Date> 
<Title>Book</Title> 
<Comment>XXX</Comment> 
</Remark> 
<Remark> 
<Cid>2009-2</Cid> 
<Date>3-12-2011 2:55:0</Date> 
<Title>Song</Title> 
<Comment>XXX</Comment> 
</Remark> 
</Remarks> 

나는 특정 <Cid>에 대한 <Comment>의 텍스트를 대체합니다. 예를 들어; Cid 2009-1, 각각의 댓글 XXX을 (를) YYY (으)로 업데이트/변경하고 싶습니다. 이 두 값은 자바 코드가 전달할 매개 변수를 기반으로합니다. 다음은 XSLT 파일입니다 -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 
    <xsl:param name="ciName" select=""/> 
    <xsl:param name="coName" select=""/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//Remarks/Remark/Comment[preceding-sibling::Cid='$ciName']"> 
     <xsl:copy> 
     <xsl:value-of select="$coName"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

그리고 이것은 부분 자바 코드 -

String cid = "2009-1"; 
    String comm = "YYY"; 

    TransformerFactory factory = TransformerFactory.newInstance(); 
    Source xslt = new StreamSource(new File("loc1.xslt")); 
    Transformer transformer = factory.newTransformer(xslt); 
    transformer.setParameter("ciName",cid); 
    transformer.setParameter("coName",comm); 

    Source text = new StreamSource(new File("Comments.xml")); 
    transformer.transform(text, new StreamResult(new File("Comments1.xml"))); 

오류 -

또한
ERROR: 'Premature end of file.' 
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.' 
Excep...... 

내가 대체/편집 할 수 없습니다입니다/변경 각각의 코멘트 ... 어떤 도움 ....? 미리 감사드립니다. 색슨 (9)와 같은 XSLT 2.0 프로세서와

답변

4

당신이

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 


    <xsl:param name="ciName" select="'2009-1'"/> 
    <xsl:param name="coName" select="'YYY'"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Remark/Comment"> 
     <xsl:copy> 
     <xsl:choose> 
      <xsl:when test="../Cid = $ciName"> 
      <xsl:value-of select="$coName"/> 
      </xsl:when> 
      <xsl:otherwise> 
      <xsl:apply-templates/> 
      </xsl:otherwise> 
     </xsl:choose> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
를 사용할 필요가 그래서 당신은 당신이 일치하는 패턴 변수 또는 매개 변수 참조를 사용할 수 없습니다

XSLT 1.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 


    <xsl:param name="ciName" select="'2009-1'"/> 
    <xsl:param name="coName" select="'YYY'"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Remark[Cid = $ciName]/Comment"> 
     <xsl:copy> 
     <xsl:value-of select="$coName"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

사용할 수 있습니다

+0

고맙습니다. 그리고 XSLT 1.0은 어떨까요? XSLT 프로세서와 함께 언급하는 것을 잊어 버렸습니다. JDK 1.7과 함께 제공되는 기본 Java XSLT 프로세서를 사용하고 있습니다. 다시 한번 고마워요. – John

+0

JDK 1.7 작업 ... 감사합니다. – John

관련 문제