简体   繁体   English

替换xsl模板

[英]Replace xsl template

I have this code: 我有以下代码:

<p>2&ensp;If the patient is unknown to the service a comprehensive assessment must be carried out prior to undertaking the procedure. (Reference <xref target="http://www.google.com" style="unformatted"/>)</p>

I want this xml to be like this: 我希望这个xml是这样的:

<p><b>2</b>If the patient is unknown to the service a comprehensive assessment must be carried out prior to undertaking the procedure. (Reference <xref target="http://www.google.com" style="unformatted"/>)</p>

By using XSL 1.0 I have tried many ways to replace string but the node (xref) is removed!! 通过使用XSL 1.0,我尝试了多种替换字符串的方法,但是删除了节点(xref)!

If you have written a template with match="p" which does the string replacement then change that to have match="p/text()" (or match="p//text()" if you need the replacement in descendants too) and then make sure you have the identity transformation template in place: 如果您编写了带有match="p"的模板来进行字符串替换,则将其更改为具有match="p/text()" (如果需要在后代中进行替换,则将其更改为match="p//text()" ),然后确保您已拥有身份转换模板:

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

It is not clear to me which substring exactly you want to replace or wrap with a b element, an example of the approach suggested above is at http://xsltransform.net/bnnZVC , it does 我不清楚您要用b元素确切替换或包装哪个子字符串,上面建议的方法示例在http://xsltransform.net/bnnZVC上 ,它确实

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template name="wrap">
    <xsl:param name="string" select="."/>
    <xsl:param name="search"/>
    <xsl:param name="wrap-name"/>
    <xsl:choose>
        <xsl:when test="not(contains($string, $search))">
            <xsl:value-of select="$string"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring-before($string, $search)"/>
            <xsl:element name="{$wrap-name}">
                <xsl:value-of select="$search"/>
            </xsl:element>
            <xsl:call-template name="wrap">
                <xsl:with-param name="string" select="substring-after($string, $search)"/>
                <xsl:with-param name="search" select="$search"/>
                <xsl:with-param name="wrap-name" select="$wrap-name"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

<xsl:template match="p/text()">
    <xsl:call-template name="wrap">
        <xsl:with-param name="search" select="'2&#8194;'"/>
        <xsl:with-param name="wrap-name" select="'b'"/>
    </xsl:call-template>
</xsl:template>

</xsl:transform>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM