首页 > 解决方案 > 在 xslt 2.0 中删除命名空间并将字符串转换为日期

问题描述

我有一个输入 xml,使用下面的 DateTransform.xslt 我可以将输入元素下的 StartDate 从字符串更改为日期格式,我还想将相同的 StartDate(日期格式)添加到所有帐户元素。我也想要删除命名空间。我是 XSLT 的新手,我尝试了以下转换,但没有获得所需的输出,有人可以帮我解决这个问题

输入.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>

日期转换.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>

输出.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>

标签: xmlxslt

解决方案


在例如上使用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">

推荐阅读