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

Joh*_*ohn 2 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>
 <Date>3-12-2011 2:55:0</Date>
 <Title>Song</Title>
 <Comment>XXX</Comment>
 </Remark>
</Remarks>
Run Code Online (Sandbox Code Playgroud)

我想替换<Comment>for specific 的文本<Cid>。例如; 对于 Cid 2009-1,我想将相应的 Comment XXX 更新/更改为 YYY。这两个值都基于我的 java 代码将传递的参数。以下是 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>
Run Code Online (Sandbox Code Playgroud)

这是部分Java 代码 -

    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")));
Run Code Online (Sandbox Code Playgroud)

错误 -

ERROR:  'Premature end of file.'
ERROR:  'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.'
Excep......
Run Code Online (Sandbox Code Playgroud)

此外,我无法编辑/替换/更改相应的评论...有什么帮助....?提前致谢。

Mar*_*nen 5

使用像 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>
Run Code Online (Sandbox Code Playgroud)

使用 XSLT 1.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>
Run Code Online (Sandbox Code Playgroud)