2016-04-14 49 views
0

输入:XSL:内更换标签值

<Remarks>Random data## B2B## abc,controls,free text ## B2B## random data</Remarks> 

的XSL应该更换

“## B2B ABC##,控制自由文本## B2B ##”

“值1备注标签:ABC,值2:控制,值3:自由文本”

所需的输出:

<Remarks>Random data,value1:abc,value2:controls,value3:free text,random data</Remarks> 

注:内## B2B##变量的数值是未知的,每次变化,现在我给样本值。

+0

向我们提供一些更改值的示例会有所帮助。现在,它只是射向黑暗。例如:可能有一个名为'value1'的值。它应该取代什么样的价值?它的分隔符是什么?到目前为止,我假设在## ## ##'字符串之间有三个由char定义的字符串(可能是','),应该由另一组三个字符串替换。这些其他字符串是如何组成的? – zx485

+0

请不要在回答问题后大幅修改您的问题。 –

回答

0

假设总是会有正好一个“占位符”由## B2B##分隔,并且它总是会包含,确切地说是 3逗号分隔的“标记”,您可以通过以下操作简单地完成:

<xsl:template match="Remarks"> 
    <xsl:copy> 
     <xsl:value-of select="substring-before(., '## B2B##')" /> 

     <xsl:variable name="placeholder" select="substring-before(substring-after(., '## B2B##'), '## B2B##')" /> 
     <xsl:text> value1: </xsl:text> 
     <xsl:value-of select="substring-before($placeholder, ',')" /> 
     <xsl:text> value2: </xsl:text> 
     <xsl:value-of select="substring-before(substring-after($placeholder, ','), ',')" /> 
     <xsl:text> value3: </xsl:text> 
     <xsl:value-of select="substring-after(substring-after($placeholder, ','), ',')" /> 

     <xsl:value-of select="substring-after(substring-after(., '## B2B##'), '## B2B##')" /> 
    </xsl:copy> 
</xsl:template> 
0

该解决方案有点复杂。它使用一个模板来提取标签和另一个标签之间的字符串以通过分隔符分隔该字符串。将什么字符串替换为其他字符串外包给包含映射的另一个文件(replacement.xml)。因为XSLT-1.0在变量中返回RTF(结果树碎片),所以替换在模板中被硬编码。在XSLT-2.0中,您可以使用一个变量来使解决方案更加灵活。

这里是replacement.xml包含映射:

<?xml version="1.0" encoding="UTF-8"?> 
<replacement> 
    <r src="abc">value1</r> 
    <r src="controls">value2</r> 
    <r src="free text">value3</r> 
</replacement> 

以及将这些映射到字符串的XSLT:

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

    <xsl:template match="/lines/Remarks"> 
     <xsl:variable name="tagName" select="'## B2B##'" /> 
     <xsl:variable name="subStrToReplace"> 
     <xsl:call-template name="strInTag"> 
      <xsl:with-param name="str" select="text()" /> 
      <xsl:with-param name="tagName" select="$tagName" /> 
     </xsl:call-template> 
     </xsl:variable> 
     <xsl:variable name="replacementString"> 
     <xsl:call-template name="splitAndReplace"> 
      <xsl:with-param name="pText" select="$subStrToReplace" /> 
     </xsl:call-template> 
     </xsl:variable> 
     <Remarks> 
     <xsl:value-of select="concat(normalize-space(substring-before(text(),$tagName)), ',',$replacementString, normalize-space(substring-after(substring-after(text(),$tagName),$tagName)))" /> 
     </Remarks> 
    </xsl:template> 

    <xsl:template name="strInTag"> 
     <xsl:param name="str" /> 
     <xsl:param name="tagName" /> 
     <xsl:value-of select="normalize-space(substring-before(substring-after($str,$tagName),$tagName))" /> 
    </xsl:template> 

    <xsl:template name="splitAndReplace"> 
     <xsl:param name="pText"/> 
     <xsl:if test="string-length($pText) > 0"> 
     <xsl:variable name="vNextItem" select="substring-before(concat($pText, ','), ',')"/> 
     <xsl:value-of select="concat($vNextItem,':',document('replacement.xml')/replacement/r[@src = $vNextItem]/text(),',')"/> 
     <xsl:call-template name="splitAndReplace"> 
      <xsl:with-param name="pText" select="substring-after($pText, ',')"/> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet>