【问题标题】:Remove namespace and convert string to date in xslt 2.0在 xslt 2.0 中删除命名空间并将字符串转换为日期
【发布时间】:2019-01-17 19:01:18
【问题描述】:

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

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>

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

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你不能简单点:

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

      【讨论】:

      • -非常感谢,它帮助了我
      • 在正则表达式中,您可以将(.{2}) 简化为(..)
      猜你喜欢
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      相关资源
      最近更新 更多