[英]Remove namespace and convert string to date in xslt 2.0
我有一個輸入xml,使用下面的DateTransform.xslt我可以將輸入元素下的StartDate從字符串更改為Date格式,我也想向所有帳戶Elements添加相同的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>
在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">
您不能簡單地做:
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')"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.