[英]Replacing the XML Node text using XSLT and Java?
XML file - 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>
I want to replace the text of <Comment>
for particular <Cid>
. 我想将
<Comment>
的文本替换为特定的<Cid>
。 For example; 例如; for Cid 2009-1, I want to update/change the respective Comment XXX to YYY.
对于Cid 2009-1,我想将相应的注释XXX更新/更改为YYY。 These both values are based on parameters, which my java code will pass.
这两个值都基于我的Java代码将传递的参数。 Following is the XSLT file -
以下是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>
And this is the part Java code - 这是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")));
Error - 错误-
ERROR: 'Premature end of file.'
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.'
Excep......
Also I am NOT able to edit/replace/change the respective comment... Any help....? 我也无法编辑/替换/更改相应的注释...有帮助...吗? Thanks in advance.
提前致谢。
With an XSLT 2.0 processor like Saxon 9 you could use 使用像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>
With XSLT 1.0 you are not allowed to use a variable or parameter reference in a match pattern so you need to use 使用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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.