删除名称空间并将字符串转换为xslt 2.0中的日期

时间:2019-01-17 08:22:46

标签: xml xslt

我有一个Input xml,使用下面的DateTransform.xslt我可以将Input Element下的StartDate从字符串更改为Date格式,我还想向所有帐户元素添加相同的StartDate(Date格式)。我也想删除名称空间。我是XSLT的新手,我尝试了以下转换,但未获得所需的输出,有人可以在此帮助我

Input.xml

<?xml version="1.0" encoding="UTF-8"?>
<Input>
  <BankName>SBI</BankName>
  <BranchCode>03</BranchCode>
  <StartDate>20080331</StartDate>
  <Account>
    <AccountName>ABC</AccountName>
    <AccountNumber>123</AccountNumber>
    <Balance>-0000123345</Balance>
  </Account>
  <Account>
    <AccountName>PQR</AccountName>
    <AccountNumber>234</AccountNumber>
    <Balance>000349015</Balance>
  </Account>
  <Account>
    <AccountName>XYZ</AccountName>
    <AccountNumber>345</AccountNumber>
    <Balance>0949710</Balance>
  </Account>
</Input>

DateTransform.xslt

  <xsl:stylesheet version="2.0"   
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" >
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="Input">
  <xsl:copy>
    <xsl:copy-of select="node()[not(self::Account)][not(self::StartDate)]"/>
           <xsl:variable name="in"><xsl:value-of select="StartDate"/> 
           </xsl:variable>
           <xsl:variable name="date" select="xs:date(concat(
            substring($in,1,4),'-',
            substring($in,5,2),'-',
            substring($in,7,2)))"/>
           <StartDate>
              <xsl:value-of select="format-date($date,'[D01]/[M01]/[Y0001]')"/>
           </StartDate>
            <Accounts>
            <xsl:apply-templates select="Account"/>
            </Accounts>
  </xsl:copy>
  </xsl:template>
  <xsl:template match="Account">
     <xsl:copy>
       <xsl:copy-of select="node()"/>
             <xsl:copy-of select="preceding-sibling::StartDate"/>
    </xsl:copy>

</xsl:template>

Output.xml

<Input>
  <BankName>SBI</BankName>
  <BranchCode>03</BranchCode>
  <StartDate 
  xmlns:xs="http://www.w3.org/2001/XMLSchema">31/03/2008</StartDate>
  <Accounts xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <Account>
        <AccountName>ABC</AccountName>
        <AccountNumber>123</AccountNumber>
        <Balance>-0000123345</Balance>
        <StartDate>20080331</StartDate>
    </Account>
    <Account>
        <AccountName>PQR</AccountName>
        <AccountNumber>234</AccountNumber>
        <Balance>000349015</Balance>
        <StartDate>20080331</StartDate>
    </Account>
    <Account>
        <AccountName>XYZ</AccountName>
        <AccountNumber>345</AccountNumber>
        <Balance>0949710</Balance>
        <StartDate>20080331</StartDate>
    </Account>
 </Accounts>
</Input>

预期输出:

<Input>
   <BankName>SBI</BankName>
   <BranchCode>03</BranchCode>
   <StartDate>31/03/2008</StartDate>
   <Accounts>
      <Account>
        <AccountName>ABC</AccountName>
        <AccountNumber>123</AccountNumber>
        <Balance>-0000123345</Balance>
        <StartDate>31/03/2008</StartDate>
      </Account>
      <Account>
        <AccountName>PQR</AccountName>
        <AccountNumber>234</AccountNumber>
        <Balance>000349015</Balance>
        <StartDate>31/03/2008</StartDate>
      </Account>
      <Account>
        <AccountName>XYZ</AccountName>
        <AccountNumber>345</AccountNumber>
        <Balance>0949710</Balance>
        <StartDate>31/03/2008</StartDate>
       </Account>
   </Accounts>
</Input>

2 个答案:

答案 0 :(得分:2)

例如在exclude-result-prefixes上使用xsl:stylesheet属性

<xsl:stylesheet version="2.0"   
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">

答案 1 :(得分:1)

您不能简单地做:

XSLT 2.0

<xsl:stylesheet version="2.0"   
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="new-date">
    <xsl:variable name="startDate" select="/Input/StartDate" />
    <xsl:value-of select="substring($startDate, 7, 2), substring($startDate, 5, 2), substring($startDate, 1, 4)" separator="/"/>
</xsl:variable>

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

<xsl:template match="StartDate">
    <xsl:copy>
        <xsl:value-of select="$new-date"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="Account">
    <xsl:copy>
        <xsl:apply-templates/>
        <StartDate>
            <xsl:value-of select="$new-date"/>    
        </StartDate>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

已添加

更短的日期格式:

<xsl:variable name="new-date" select="replace(/Input/StartDate, '(.{4})(.{2})(.{2})', '$3/$2/$1')"/>