连接字符串包括重复标记

时间:2014-07-31 06:39:30

标签: xml xslt-1.0

我有这个输入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:

应具有此输出
<?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标记的值:

  • 一个。连接字符串&#39; CUST&#39; +客户/姓名+客户/地址
  • 湾连接字符串&#39; ACCT&#39; + CustomerAcct / AcctName + CustomerAcct / AcctNumber
  • ℃。连接字符串&#39; ADDL&#39; AddlInfo / Info1(1..n)
  • 下的+值
  • 连接a + b + c .... etc

我希望有人可以帮助我。谢谢大家

BTW我在XSLT 1.0上需要它

1 个答案:

答案 0 :(得分:1)

看看这是否有帮助:

<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()函数,例如:

<xsl:value-of select="concat('CUST+', urn:EntryDtls/urn:TransactionDetails/urn:Parties/urn:Customer/urn:Name)" />