首页 > 解决方案 > XSLT:不允许将多个项目的序列作为 fn:substring() 的第一个参数

问题描述

从下面的 XML 中,我尝试替换/ABC//DEF/,但我遇到了错误。

错误:不允许将多个项目的序列作为 fn:substring() 的第一个参数

XML:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
   <CstmrCdtTrfInitn>
      <GrpHdr>
         <MsgId>fhjfdgh</MsgId>
         <NbOfTxs>2</NbOfTxs>
      </GrpHdr>
      <PmtInf>
         <PmtInfId>21351</PmtInfId>
         <NbOfTxs>2</NbOfTxs>
         <CdtTrfTxInf>
            <PmtId>
               <InstrId>124512</InstrId>
               <EndToEndId>24523q</EndToEndId>
            </PmtId>
            <InstrForDbtrAgt>/ABC/245334524362356235</InstrForDbtrAgt>
         </CdtTrfTxInf>
         <CdtTrfTxInf>
            <PmtId>
               <InstrId>45234</InstrId>
               <EndToEndId>42353</EndToEndId>
            </PmtId>
             <InstrForDbtrAgt>/ABC/111334524362356235</InstrForDbtrAgt>
         </CdtTrfTxInf>
      </PmtInf>
   </CstmrCdtTrfInitn>
</Document>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:cc1="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="cc1" version="2.0">    

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/cc1:Document/cc1:CstmrCdtTrfInitn/cc1:PmtInf/cc1:CdtTrfTxInf/cc1:InstrForDbtrAgt">
        <xsl:element namespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" name="InstrForDbtrAgt">
            <xsl:variable name="InstrForDbtrAgtInv" select="substring(../../cc1:CdtTrfTxInf/cc1:InstrForDbtrAgt, 7, 200)"/>
            <xsl:value-of select="concat('/DEF/', $InstrForDbtrAgtInv)"/>
        </xsl:element>
    </xsl:template> 

</xsl:stylesheet>

标签: xmlxslt

解决方案


我正在尝试用 /DEF/ 替换 /ABC/

我认为您将非常简单的事情过度复杂化了:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="InstrForDbtrAgt">
    <xsl:copy>
        <xsl:value-of select="replace(., '/ABC/', '/DEF/')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

推荐阅读