使用XSLT和Java替换XML节点文本?

使用XSLT和Java替换XML节点文本?,java,xml,xslt,Java,Xml,Xslt,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> <

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>
错误-

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

此外,我无法编辑/替换/更改相应的注释。。。有什么帮助吗。。。。?提前感谢。

使用类似Saxon 9的XSLT 2.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>

在XSLT1.0中,不允许在匹配模式中使用变量或参数引用,因此需要使用

<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>


非常感谢,亲爱的。那么XSLT1.0呢。我忘了在提到XSLT处理器时提到它。我使用的是JDK1.7附带的默认Java XSLT处理器。非常感谢您再次使用JDK1.7。。。非常感谢亲爱的:)
<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>
<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>