我正在开发一个解析字符串测试(
<root></root>
内的txt)的xslt,我想要那个:
<xsl:if test="starts-with(., ' 61')">
<xsl:variable name="IBAN" select="concat(substring(.,100,4),substring(.,52,23))" />
<Stmt>
<Acct>
<Id>
<IBAN><xsl:value-of select="$IBAN"/></IBAN>
</Id>
</Acct>
</Stmt>
</xsl:if>
但是先例我已经在另一个 if 中创建了这个标签: 问题很简单:如何在优先父标签中插入标签?
我的代码是这样的:
<xsl:template match="/">
<xsl:copy copy-namespaces="no">
<xsl:variable name="parts" select="str:split(root, ' RH')"/>
<root>
<xsl:for-each select="$parts">
<xsl:variable name="part" select="concat(' RH', .)" />
<xsl:variable name="lines" select="str:tokenize($part, '
')"/>
<Document>
<BkToCstmrStmt>
<GrpHdr>
<xsl:for-each select="$lines">
<xsl:if test="starts-with(., ' RH')">
<Stmt>
<Id><xsl:value-of select="concat(normalize-space(substring(., 20, 20)),'-1')"/></Id> <!-- perchè -1 possono esseci piu id? -->
<ElctrncSeqNb>1</ElctrncSeqNb> <!-- Progressivo di rendicontazione elettronica da verificare come si vuole fare-->
<CreDtTm><xsl:value-of select="concat($year, '-', $month, '-', $day, 'T', $hour, ':', $minute, ':', $second, 'Z')"/></CreDtTm>
</Stmt>
</xsl:if>
<xsl:if test="starts-with(., ' 61')">
<xsl:variable name="IBAN" select="concat(substring(.,100,4),substring(.,52,23))" />
<Stmt>
<Acct>
<Id>
<IBAN><xsl:value-of select="$IBAN"/></IBAN>
</Id>
</Acct>
</Stmt>
</xsl:if>
</xsl:for-each>
</GrpHdr>
</BkToCstmrStmt>
</Document>
</xsl:for-each>
</root>
</xsl:copy>
</xsl:template>
现在第二个 if 中的代码覆盖第一个 if 中的第一个
<STMT>
。我只想插入。
输入xml:
<root>
RH03051CDSIA280524CM1490301951171
610000001 93001 G0305101700000000004575EUR270524C000000000000,00IT44
620000001001270524270524D000000000649,3450TE ITDA00DPN145
630000001001HAYS SRL/AVIS BUDGET ITALIA S.P.A./AR885265/2355070853/B2B/RCUR/OE5OA5200P4907R3
640000001EUR270524C000000000000,00
EF03051CDSIA280524CM1490301951171 0000001 0000006
RH03051CDSIA280524CM1490301951349
610000001 93001 Z0305101699000078389249USD270524C000000001320,97IT72
640000001USD270524C000000001320,97
EF03051CDSIA280524CM1490301951349 0000001 0000004
</root>
我想要转换后的xml:
<root>
<Document>
<BkToCstmrStmt>
<GrpHdr>
<Stmt>
<Id>CM1490301951171-1</Id>
<ElctrncSeqNb>1</ElctrncSeqNb>
<CreDtTm>2024-05-28T12:08:29Z</CreDtTm>
<Acct>
<Id>
<IBAN>IT44G0305101700000000004575</IBAN>
</Id>
</Acct>
</Stmt>
</GrpHdr>
</BkToCstmrStmt>
</Document>
</root>
但是我得到了这个:
<root>
<Document>
<BkToCstmrStmt>
<GrpHdr>
<Stmt>
<Id>CM1490301951171-1</Id>
<ElctrncSeqNb>1</ElctrncSeqNb>
<CreDtTm>2024-05-28T12:08:29Z</CreDtTm>
</Stmt>
<Stmt>
<Acct>
<Id>
<IBAN>IT44G0305101700000000004575</IBAN>
</Id>
</Acct>
</Stmt>
</GrpHdr>
</BkToCstmrStmt>
</Document>
</root>
有什么建议给我吗? 谢谢 问候
您的代码不完整,并且在未定义的变量上出错:
<CreDtTm><xsl:value-of select="concat($year, '-', $month, '-', $day, 'T', $hour, ':', $minute, ':', $second, 'Z')"/></CreDtTm>
AFAICT,您的输入应产生 3 个
Document
元素,但您的预期输出仅显示一个。
这不是一个最小的例子。
我猜(!)你想做类似的事情:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="parts" select="str:split(root, ' RH')"/>
<root>
<xsl:for-each select="$parts">
<xsl:variable name="part" select="concat(' RH', .)" />
<xsl:variable name="lines" select="str:tokenize($part, '
')"/>
<xsl:variable name="line61" select="$lines[starts-with(., ' 61')]"/>
<Document>
<BkToCstmrStmt>
<GrpHdr>
<Stmt>
<Id>
<xsl:value-of select="concat(normalize-space(substring($lines[1], 20, 20)),'-1')"/>
</Id>
<ElctrncSeqNb>1</ElctrncSeqNb>
<CreDtTm>
<!-- ??? missing variables ??? -->
</CreDtTm>
<xsl:variable name="line61" select="$lines[starts-with(., ' 61')]"/>
<xsl:if test="$line61">
<Acct>
<Id>
<IBAN>
<xsl:value-of select="concat(substring(concat(substring($line61, 100, 4), substring($line61, 52, 23)),100, 4), substring($line61, 52, 23))"/>
</IBAN>
</Id>
</Acct>
</xsl:if>
</Stmt>
</GrpHdr>
</BkToCstmrStmt>
</Document>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>