[英]use multiple in <xsl:param name= select> xsl
I have the following code: 我有以下代码:
<xsl:variable name="landlord"><xsl:value-of select="agreement/landlord1name"/></xsl:variable>
<xsl:variable name="tenant"><xsl:value-of select="agreement/tenantname"/></xsl:variable>
<xsl:variable name="property"><xsl:value-of select="agreement/propertyname"/></xsl:variable>
<xsl:variable name="startdate"><xsl:value-of select="agreement/startdate"/></xsl:variable>
<xsl:variable name="period"><xsl:value-of select="agreement/agreementterm"/></xsl:variable>
<xsl:variable name="lbody">
<xsl:call-template name="replace-text-multi">
<xsl:with-param name="text" select="tatemplate/letterbody"/>
<xsl:with-param name="replaces" select="'#tenant #period'"/>
<xsl:with-param name="bys" select="$tenant"/>
<xsl:with-param name="delim" select="' '"/>
</xsl:call-template>
</xsl:variable>
This is the code for template name=replace-text: 这是模板名称=替换文本的代码:
<xsl:template name='replace-text'>
<xsl:param name='text'/>
<xsl:param name='replace'/>
<xsl:param name='by'/>
<xsl:choose>
<xsl:when test='contains($text, $replace)'>
<xsl:value-of select='substring-before($text, $replace)'/>
<xsl:value-of select='$by' disable-output-escaping='yes'/>
<xsl:call-template name='replace-text'>
<xsl:with-param name='text' select='substring-after($text, $replace)'/>
<xsl:with-param name='replace' select='$replace'/>
<xsl:with-param name='by' select='$by'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$text'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This is the code for template name=replace-text-multi: 这是模板名称= replace-text-multi的代码:
<xsl:template name="replace-text-multi">
<xsl:param name="text"/>
<xsl:param name="replaces"/>
<xsl:param name="bys"/>
<xsl:param name="delim"/>
<xsl:choose>
<xsl:when test="contains($replaces, $delim)">
<xsl:variable name="textSoFar">
<xsl:call-template name="replace-text-multi">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replaces" select="substring-after($replaces, $delim)"/>
<xsl:with-param name="bys" select="substring-after($bys, $delim)"/>
<xsl:with-param name="delim" select="$delim"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace-text">
<xsl:with-param name="text" select="$textSoFar"/>
<xsl:with-param name="replace" select="substring-before($replaces, $delim)"/>
<xsl:with-param name="by" select="substring-before($bys, $delim)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="replace-text">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="$replaces"/>
<xsl:with-param name="by" select="$bys"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
here I want to replace #tenant as $tenant and #period as $period but xsl cannot read select="$tenant $period". 这里我想将#tenant替换为$ tenant,将#period替换为$ period,但是xsl无法读取select =“ $ tenant $ period”。 Someone please help 有人请帮忙
I think the problem is in your <xsl:when test="contains($replaces, $delim)">
condition. 我认为问题出在您的<xsl:when test="contains($replaces, $delim)">
条件下。 You need to call the replace-text
first, and then pass the output of that to the replace-text-multi
template with the remaining parameters. 您需要先调用replace-text
,然后将其输出与其余参数一起传递给replace-text-multi
模板。
<xsl:when test="contains($replaces, $delim)">
<xsl:variable name="textSoFar">
<xsl:call-template name="replace-text">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="substring-before($replaces, $delim)"/>
<xsl:with-param name="by" select="substring-before($bys, $delim)"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace-text-multi">
<xsl:with-param name="text" select="$textSoFar"/>
<xsl:with-param name="replaces" select="substring-after($replaces, $delim)"/>
<xsl:with-param name="bys" select="substring-after($bys, $delim)"/>
<xsl:with-param name="delim" select="$delim"/>
</xsl:call-template>
</xsl:when>
It might also be worth making your delimiters a different string, other than space, just in case your replacement strings have spaces in. You also need to change how you set the bys
(where $delim
is a variable containing your chosen delimiter) 为了防止替换字符串中包含空格,也可能需要将分隔符设置为除空格以外的其他字符串。以防万一替换字符串中包含空格。您还需要更改bys
设置方式( $delim
是包含所选分隔符的变量)
<xsl:with-param name="bys" select="concat($tenant, $delim, $period)"/>
Try this XSLT 试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/root">
<xsl:variable name="tenant"><xsl:value-of select="agreement/tenantname"/></xsl:variable>
<xsl:variable name="period"><xsl:value-of select="agreement/agreementterm"/></xsl:variable>
<xsl:variable name="delim" select="'||'" />
<xsl:call-template name="replace-text-multi">
<xsl:with-param name="text" select="tatemplate/letterbody"/>
<xsl:with-param name="replaces" select="concat('#tenant', $delim, '#period')"/>
<xsl:with-param name="bys" select="concat($tenant, $delim, $period)"/>
<xsl:with-param name="delim" select="$delim"/>
</xsl:call-template>
</xsl:template>
<xsl:template name='replace-text'>
<xsl:param name='text'/>
<xsl:param name='replace'/>
<xsl:param name='by'/>
<xsl:choose>
<xsl:when test='contains($text, $replace)'>
<xsl:value-of select='substring-before($text, $replace)'/>
<xsl:value-of select='$by' disable-output-escaping='yes'/>
<xsl:call-template name='replace-text'>
<xsl:with-param name='text' select='substring-after($text, $replace)'/>
<xsl:with-param name='replace' select='$replace'/>
<xsl:with-param name='by' select='$by'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$text'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="replace-text-multi">
<xsl:param name="text"/>
<xsl:param name="replaces"/>
<xsl:param name="bys"/>
<xsl:param name="delim"/>
<xsl:choose>
<xsl:when test="contains($replaces, $delim)">
<xsl:variable name="textSoFar">
<xsl:call-template name="replace-text">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="substring-before($replaces, $delim)"/>
<xsl:with-param name="by" select="substring-before($bys, $delim)"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace-text-multi">
<xsl:with-param name="text" select="$textSoFar"/>
<xsl:with-param name="replaces" select="substring-after($replaces, $delim)"/>
<xsl:with-param name="bys" select="substring-after($bys, $delim)"/>
<xsl:with-param name="delim" select="$delim"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="replace-text">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="$replaces"/>
<xsl:with-param name="by" select="$bys"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
When applied to this XML 应用于此XML时
<root>
<agreement>
<tenantname>Tenant Name</tenantname>
<agreementterm>1st Dec to 2nd Dec</agreementterm>
</agreement>
<tatemplate>
<letterbody>
Dear #tenant
You term is #period
Kind regards
</letterbody>
</tatemplate>
</root>
The following is output 输出如下
Dear Tenant Name
You term is 1st Dec to 2nd Dec
Kind regards
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.