いくつかのxml値を置き換えることができるxslテンプレートがあります。ここで、これらの値をコードによって動的に生成する必要があります。
Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("foo.xsl"));
trans.transform(new StreamSource(new File("foo.xml"), new StreamResult(new File("output.xml")));
id = 1の場合にのみ名前を置き換えるなど、どうすれば取得できますか?さらに、ハードコードされていないjavacodeによって動的にそのIDを提供しますか?
<?xml version="1.0"?>
<my:accounts xmlns:my="http://myns">
<my:account>
<my:name>alex</my:name>
<my:id>1</my:id>
</my:account>
<my:account>
<my:name>Fiona</my:name>
<my:id>2</my:id>
</my:account>
</my:accounts>
これにより、すべての名前が置き換えられます。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://myns">
<xsl:param name="propertyName" select="'alex'"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name()='account']/*[local-name()='name']/text()[.='{$propertyName}']">
<xsl:text>johndoe</xsl:text>
</xsl:template>
</xsl:stylesheet>