I want to use parameter value in xpath of xsl select to replace text. But, the value not getting replaced.
xml
<?xml version="1.0"?>
<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:id="1" w:rsidR="00041C05" w:rsidRDefault="00EA4F36">
<w:r>
<w:t>value1</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00041C05" w:rsidSect="00041C05">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
xsl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
exclude-result-prefixes="w"
>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:param name="replacement">
new value
</xsl:param>
<xsl:param name="id">1</xsl:param>
<xsl:template match='w:p[@w:id="$id"]/w:r/w:t/text()'>
<xsl:value-of select="$replacement" />
</xsl:template>
</xsl:stylesheet>
output
<?xml version="1.0" encoding="UTF-8"?><w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<w:p w:id="1" w:rsidR="00041C05" w:rsidRDefault="00EA4F36">
<w:r>
<w:t>value1</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="00041C05" w:rsidSect="00041C05">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
<w:t>value1</w:t>
should replaced to <w:t>new value</w:t>
.
Code
"$id"
is a string literal for the string $id
. It's not a variable/parameter reference.
You can't use a variable/parameter reference in a match
expression in XSLT 1.0, so I think your best bet would be to check within the xsl:template
whether the text node is the one you're looking for:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
exclude-result-prefixes="w"
>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:param name="replacement">
new value
</xsl:param>
<xsl:param name="id">1</xsl:param>
<xsl:template match='w:p/w:r/w:t/text()'>
<xsl:choose>
<xsl:when test='../../../@w:id = $id'>
<xsl:value-of select="$replacement" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>