Concatenate Strings including repeating tag

我的梦境 提交于 2019-12-25 13:10:18

问题


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>

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>

Im not sure how I can use variable or any function to store the value of Description tag with this requirements:

  • a. Concatenate strings 'CUST' + Customer/Name + Customer/Address
  • b. Concatenate strings 'ACCT' + CustomerAcct/AcctName + CustomerAcct/AcctNumber
  • c. Concatenate strings 'ADDL' + values there is under AddlInfo/Info1 (1..n)
  • etc
  • Concatenate a+b+c .... etc

I hope someone can help me with this. Thanks everyone

BTW I need it on XSLT 1.0


回答1:


See if this helps:

<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>

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)" />


来源:https://stackoverflow.com/questions/25052304/concatenate-strings-including-repeating-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!