Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将唯一字符串替换为<;xml>;清理xslt中的标记_Xml_Xslt - Fatal编程技术网

将唯一字符串替换为<;xml>;清理xslt中的标记

将唯一字符串替换为<;xml>;清理xslt中的标记,xml,xslt,Xml,Xslt,我有一条xml消息——由一个相当基本的xml编写器创建(在某些情况下它不会迭代重复记录)——这意味着我们有一个文本字符串,其中必然包含分隔的文本块。 在XML工具中,我们能够编写一个很长的文本字符串,但我们能够添加行末/新行指示符。(TXNNTX,但可以是除xml标记以外的任何内容) 作为一个字符串,它可以在XML文件的任何地方找到 字符串将是: <causale>This is text that we need in line one TXNNTX We then need te

我有一条xml消息——由一个相当基本的xml编写器创建(在某些情况下它不会迭代重复记录)——这意味着我们有一个文本字符串,其中必然包含分隔的文本块。 在XML工具中,我们能够编写一个很长的文本字符串,但我们能够添加行末/新行指示符。(TXNNTX,但可以是除xml标记以外的任何内容) 作为一个字符串,它可以在XML文件的任何地方找到

字符串将是:

<causale>This is text that we need in line one TXNNTX We then need test to follow again that may have something important to say TXNNTX Then another line which is longer and longer and longer and longer and longer and longer and longer TXNNTX The maybe shorter TXNNTX or dots TXNNTX .............................................................................</causale>
这是我们在第一行TXNNTX中需要的文本,然后我们需要再次进行测试,可能有一些重要的内容要说TXNNTX,然后是另一行,它越来越长,越来越长,越来越长,越来越长,越来越长TXNNTX可能越短,或者点TXNNTX.............................................................................
我们需要找到TXNNTX并将其替换为,以便输出变为(新行仅用于说明):

这是我们第一行需要的文本
然后,我们需要再次进行测试,可能会有重要的事情要说
然后是另一条越来越长的线,越来越长,越来越长,越来越长
可能短一点
还是点
.............................................................................
字符数最多可达2000个字符
尝试了许多不同的脚本,但可能是因为我们正在使用内部的“”,而混淆了我对使用XSLT 3编写脚本的基本理解,您可以将
分析字符串
函数的结果提供给以结尾的每个组的

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output indent="yes"/>

  <xsl:param name="marker" as="xs:string">TXNNTX</xsl:param>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="causale">
      <xsl:for-each-group select="analyze-string(., $marker)/*" group-ending-with="fn:match">
          <causale>
              <xsl:value-of select="current-group()[not(self::fn:match)]"/>
          </causale>
      </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

TXNNTX

或使用不同的模式处理功能的结果:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output indent="yes"/>

  <xsl:param name="marker" as="xs:string">TXNNTX</xsl:param>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:mode name="wrap" on-no-match="shallow-skip"/>

  <xsl:template match="causale">
      <xsl:apply-templates select="analyze-string(., $marker)" mode="wrap"/>
  </xsl:template>

  <xsl:template mode="wrap" match="fn:non-match">
      <causale>
          <xsl:apply-templates/>
      </causale>
  </xsl:template>

</xsl:stylesheet>

TXNNTX

在XSLT 2.0或更高版本中,通过使用
tokenize()
函数,可以非常简单地完成此操作:

<xsl:for-each select="tokenize(causale, 'TXNNTX')">
    <causale>
        <xsl:value-of select="."/>
    </causale>
</xsl:for-each>



如果仅限于XSLT1.0,请检查处理器是否支持EXSLT扩展功能。如果没有,请使用如图所示的递归模板。

对于XSLT 2,这是
xsl:analyze string
的工作,而对于XSLT 3,您可以使用
analyze string
函数更容易地完成。了解是否可以使用XSLT或3处理器,如Saxon 9。
<xsl:for-each select="tokenize(causale, 'TXNNTX')">
    <causale>
        <xsl:value-of select="."/>
    </causale>
</xsl:for-each>