从字符串中删除重复值

从字符串中删除重复值

本文介绍了从字符串中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 xslt 删除这个 xml 中的文件路径

I need to remove the filepath in this xmlvia xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
    <InvoiceCapture>
    <Invoice>
        <CaptureDate>2014-02-19</CaptureDate>
        <CaptureTime>14:04:07</CaptureTime>
        <Company>bygg</Company>
        <Type>0</Type>
        <Supplier>11111111</Supplier>
        <SupplierInvoiceNo>11111111</SupplierInvoiceNo>
        <InvoiceDate>2013-12-30</InvoiceDate>
        <DueDate>2014-01-29</DueDate>
        <Reference1>11111111</Reference1>
        <Reference2>11111111</Reference2>
        <Currency>SEK</Currency>
        <Amount>11111111</Amount>
        <VatAmount>11111111</VatAmount>
        <AlternativeID>20140219_bygg_2788</AlternativeID>
        <ImageFile>\\extsql1\INVOICES\m3Bygg\test\2KB16000.PNG  \\extsql1\INVOICES\m3Bygg\test\2KB16002.PNG \\extsql1\INVOICES\m3Bygg\test\2KB16004.PNG \\extsql1\INVOICES\m3Bygg\test\2KB16006.PNG \\extsql1\INVOICES\m3Bygg\test\2KB16008.PNG</ImageFile>
        <NoOfImages>5</NoOfImages>
        <BatchPrefix/>
        <BatchNo>2788</BatchNo>
        <InvoiceLine/>
    </Invoice>
</InvoiceCapture>

我需要的输出只是用空格分隔的图像名称:

The output I need is only the image names separated with space:

<ImageFile>2JE04000.PNG 2JE04002.PNG 2JE04004.PNG 2JE04006.PNG 2JE04008.PNG</ImageFile>

推荐答案

我的建议是这个模板,我称之为 extract-substrings-between.它的优点是使用单个模板完成任务,不需要扩展,这些扩展不是针对这个实际问题的,而且更普遍有用.

My suggestion is this template that I called extract-substrings-between. It has the advantage of doing the task with a single template not requiring extensions that is not specific to this actual problem and more generally useful.

它的参数是:

  • string: 待处理的字符串.它默认为应用了 normalize-space() 的当前节点的值.
  • startCharacterendCharacter: 模板提取既不包含 startCharacter 也不包含 的任何子字符串endCharacter,但紧跟在 startCharacter 或字符串的开头之后,紧接着是 endCharacter 或字符串的结尾.startCharacterendCharacter 默认为空格.
  • outputSeparator: 顾名思义,在输出中分隔提取的子字符串的字符串.也默认为空格.
  • string: The to-be-processed string. It defaults to the value of the current node with normalize-space() applied to it.
  • startCharacter and endCharacter: The template extracts any substring that neither contains a startCharacter nor an endCharacter, but is immediately preceded by either a startCharacter or the start of the string and immediately followed by either the endCharacter or the end of the string. Both startCharacter and endCharacter default to a space.
  • outputSeparator: As the name says, the string separating the extracted substrings on output. Defaults to a space as well.

样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <xsl:for-each select="InvoiceCapture/Invoice/ImageFile">
      <xsl:copy>
        <xsl:call-template name="extract-substrings-between">
          <xsl:with-param name="startCharacter" select="'\'"/>
        </xsl:call-template>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="extract-substrings-between">
    <xsl:param name="string" select="normalize-space()"/>
    <xsl:param name="startCharacter" select="' '"/>
    <xsl:param name="endCharacter" select="' '"/>
    <xsl:param name="outputSeparator" select="' '"/>

    <xsl:variable name="currentToken"
      select="substring-before(concat($string, $endCharacter), $endCharacter)"/>

    <xsl:choose>
      <xsl:when test="contains($currentToken, $startCharacter)">
        <!-- We need to chip off more from the current token -->
        <xsl:call-template name="extract-substrings-between">
          <xsl:with-param name="string" select="substring-after($string, $startCharacter)"/>
          <xsl:with-param name="startCharacter" select="$startCharacter"/>
          <xsl:with-param name="endCharacter" select="$endCharacter"/>
          <xsl:with-param name="outputSeparator" select="$outputSeparator"/>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <!-- We've isolated what we want to return from the current token -->
        <xsl:value-of select="$currentToken"/>

        <xsl:variable name="remainingString" select="substring-after($string, ' ')"/>
        <xsl:if test="$remainingString != ''">
          <xsl:value-of select="$outputSeparator"/>
          <xsl:call-template name="extract-substrings-between">
            <xsl:with-param name="string" select="$remainingString"/>
            <xsl:with-param name="startCharacter" select="$startCharacter"/>
            <xsl:with-param name="endCharacter" select="$endCharacter"/>
            <xsl:with-param name="outputSeparator" select="$outputSeparator"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

输出:

<ImageFile>2KB16000.PNG 2KB16002.PNG 2KB16004.PNG 2KB16006.PNG 2KB16008.PNG</ImageFile>

这篇关于从字符串中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:30