连接字符串,包括重复标签 [英] Concatenate Strings including repeating tag
本文介绍了连接字符串,包括重复标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个输入 XML:
I have this Input XML:
<?xml version='1.0' encoding='UTF-8'?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn-sample">
<CustomerRecord>
<Statement>
<StmtId>123</StmtId>
<StmtDate>2013-08-16</StmtDate>
<AcctNumber>123456789</AcctNumber>
<Balance>
<Type>OP</Type>
<Amount>1.00</Amount>
<CreditOrDebit>DR</CreditOrDebit>
<Date>2013-08-15</Date>
</Balance>
<Balance>
<Type>CL</Type>
<Amount>2.00</Amount>
<CreditOrDebit>CR</CreditOrDebit>
<Date>2013-08-16</Date>
</Balance>
<Balance>
<Type>FW</Type>
<Amount>3.00</Amount>
<CreditOrDebit>CR</CreditOrDebit>
<Date>2013-08-17</Date>
</Balance>
<Entry>
<Amount>7778.70</Amount>
<CreditOrDebit>DR</CreditOrDebit>
<EntryDtls>
<TransactionDetails>
<Parties>
<Customer>
<Name>Customer 1 Name</Name>
<Address>Address Line 1</Address>
</Customer>
<CustomerAcct>
<AcctName>Account Name 1</AcctName>
<AcctNumber>12345677</AcctNumber>
</CustomerAcct>
</Parties>
<AddlInfo>
<Info1>Info 1</Info1>
<Info1>Info 2</Info1>
<Info1>Info 3</Info1>
</AddlInfo>
</TransactionDetails>
</EntryDtls>
</Entry>
</Statement>
</CustomerRecord>
</Document>
应该有描述标签 CStatement/CStatementLine/Description 的输出:
That should have this output for the Description tag CStatement/CStatementLine/Description:
<?xml version="1.0" encoding="utf-8"?>
<Root> <!--should have no attribute-->
<CStatement>
<CStatementId>123</CStatementId>
<CStatementDate>2013-08-16</CStatementDate>
<AccountNumber>123456789</AccountNumber>
<OpeningBalance>-1.00</OpeningBalance>
<ClosingBalance>2.00</ClosingBalance>
<CStatementLine>
<DebitOrCredit>DR</DebitOrCredit>
<Amount>-7778.70</Amount>
<CustomerName>Customer 1 Name</CustomerName>
<CustomerBankAccount>12345677</CustomerBankAccount>
<Description>CUST+Customer 1 Name+Address Line 2+ACCT+Account Name 1+12345677+ADDL+Info 1+Info 2+Info 3</Description>
</CStatementLine>
</CStatement>
</Root>
我不确定如何使用变量或任何函数来存储具有此要求的 Description 标记的值:
Im not sure how I can use variable or any function to store the value of Description tag with this requirements:
- a.连接字符串 'CUST' + Customer/Name + Customer/Address
- b.连接字符串 'ACCT' + CustomerAcct/AcctName + CustomerAcct/AcctNumber
- c.连接字符串 'ADDL' + AddlInfo/Info1 (1..n) 下的值
- 等
- 连接 a+b+c .... 等
希望有人能帮我解决这个问题.谢谢大家
I hope someone can help me with this. Thanks everyone
顺便说一句,我在 XSLT 1.0 上需要它
BTW I need it on XSLT 1.0
推荐答案
看看是否有帮助:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:urn="urn-sample"
exclude-result-prefixes="urn">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="/urn:Document">
<Root>
<xsl:apply-templates select="urn:CustomerRecord/urn:Statement" />
</Root>
</xsl:template>
<xsl:template match="urn:Statement">
<CStatement>
<CStatementId>
<xsl:value-of select="urn:StmtId"/>
</CStatementId>
<!-- more -->
<xsl:apply-templates select="urn:Entry" />
</CStatement>
</xsl:template>
<xsl:template match="urn:Entry">
<StatementLine>
<Description>
<xsl:text>CUST+</xsl:text>
<xsl:value-of select="urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:Customer/urn:Name" />
<xsl:text>+</xsl:text>
<xsl:value-of select="urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:Customer/urn:Address" />
<!-- more of the same -->
<xsl:text>+ADDL+</xsl:text>
<xsl:for-each select="urn:EntryDtls/urn:TransactionDetails/urn:AddlInfo/urn:Info1">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">
<xsl:text>+</xsl:text>
</xsl:if>
</xsl:for-each>
</Description>
</StatementLine>
</xsl:template>
</xsl:stylesheet>
您也可以使用 concat() 函数,例如:
You can also use the concat() function, e.g:
<xsl:value-of select="concat('CUST+', urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:Customer/urn:Name)" />
这篇关于连接字符串,包括重复标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文