xml - 在 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>
解决方案
在例如上使用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">
推荐阅读
- c# - Automapper 从 v8 迁移到 v9 后使用 ProjectTo 时出现“参数类型不匹配”
- eclipse - 在 Windows 资源管理器文件子菜单中添加“使用 Eclipse 打开”
- python - 无法使用 python 从 sqlite 数据库中获取数据
- c# - 如何避免在我的 Wix Sharp 设置项目中添加 *.g.wxs 文件?
- html - 如何对 CSS3 硬件加速 translate3d 进行基准测试,我应该将其应用于 body 元素吗?
- r - 带有标签的李克特数据的 R 频率表
- javascript - 如何使用具有反应形式的对象?
- javascript - 我可以在生产中使用 getDisplaymedia 吗?
- javascript - Vue.js--更改 div 元素的大小,el.style.attr 不起作用
- angular - 错误类型错误:无法在 NGRX 效果中读取 null 的属性“管道”