xslt - 如何使用 xslt 添加根元素?
问题描述
我有一个 XML 文件,我只想保留以下标签:
assetId, index, basis1, rate, spread, pors, matDate, notional, currNotional, fixingDate and intDays
以下是原始 XML 文件的示例:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<messageDetail>
<transaction>
<trades>
<trade>
<tradeInfoString>SPOT_FX</tradeInfoString>
<tradeStatusCode>AMENDED</tradeStatusCode>
<enteredDateTime>2019-05-09T10:49:05+01:00</enteredDateTime>
<reference>3956502P</reference>
<version>4</version>
<tradeDate>2016-06-24</tradeDate>
<tradeTypeCode>MARKET</tradeTypeCode>
<tradeLegs>
<tradeLeg>
<bookId>721</bookId>
<assetId>001FM1C9</assetId>
<index>FIXED</index>
<basis1>30/360</basis1>
<rate>-0.381</rate>
<spread>0</spread>
<pors>Sale</pors>
<matDate>2019-05-09</matDate>
<notional>0.00</notional>
<currNotional>25000000.00</currNotional>
<transfers>
<transfer>
<transferType>INTEREST</transferType>
<longShort>LONG</longShort>
<amount>82285.42</amount>
<settlementDate>2017-05-09</settlementDate>
<fixingDate>9999-12-31</fixingDate>
<intDays>311</intDays>
<instrument>
<type>CURRENCY</type>
<currency>EUR</currency>
</instrument>
</transfer>
<transfer>
<transferType>INTEREST</transferType>
<longShort>LONG</longShort>
<amount>95250.00</amount>
<settlementDate>2018-05-09</settlementDate>
<fixingDate>9999-12-31</fixingDate>
<intDays>360</intDays>
<instrument>
<type>CURRENCY</type>
<currency>EUR</currency>
</instrument>
</transfer>
</transfers>
</tradeLeg>
<tradeLeg>
<bookId>721</bookId>
<assetId>001FM1CB</assetId>
<index>FORM</index>
<basis1>A360</basis1>
<rate>0</rate>
<spread>0</spread>
<pors>Purchase</pors>
<matDate>2019-05-09</matDate>
<notional>0.00</notional>
<currNotional>25000000.00</currNotional>
<transfers>
<transfer>
<transferType>INTEREST</transferType>
<longShort>SHORT</longShort>
<amount>10150.00</amount>
<settlementDate>2016-08-09</settlementDate>
<fixingDate>2016-06-24</fixingDate>
<intDays>42</intDays>
<instrument>
<type>CURRENCY</type>
<currency>EUR</currency>
</instrument>
</transfer>
</transfers>
</tradeLeg>
</tradeLegs>
<endDate>2019-05-09</endDate>
<startDate>2016-06-28</startDate>
<systemProductCode>SWAP</systemProductCode>
</trade>
</trades>
</transaction>
</messageDetail>
为此,我使用了以下 XSLT 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hsbcfixml="http://www.fixprotocol.org/FIXML-4-4"
exclude-result-prefixes="hsbcfixml">
<xsl:output method="xml" encoding="UTF-8"
indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template
match="node()[not(self::tradeLeg or ancestor-or-self::assetId or ancestor-or-self::index or ancestor-or-self::basis1 or ancestor-or-self::rate or ancestor-or-self::spread or ancestor-or-self::pors or ancestor-or-self::matDate or ancestor-or-self::notional or ancestor-or-self::currNotional or ancestor-or-self::fixingDate or ancestor-or-self::intDays)]">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
这产生了以下输出:
<tradeLeg>
<assetId>001FM1C9</assetId>
<index>FIXED</index>
<basis1>30/360</basis1>
<rate>-0.381</rate>
<spread>0</spread>
<pors>Sale</pors>
<matDate>2019-05-09</matDate>
<notional>0.00</notional>
<currNotional>25000000.00</currNotional>
<fixingDate>9999-12-31</fixingDate>
<intDays>311</intDays>
<fixingDate>9999-12-31</fixingDate>
<intDays>360</intDays>
</tradeLeg>
<tradeLeg>
<assetId>001FM1CB</assetId>
<index>FORM</index>
<basis1>A360</basis1>
<rate>0</rate>
<spread>0</spread>
<pors>Purchase</pors>
<matDate>2019-05-09</matDate>
<notional>0.00</notional>
<currNotional>25000000.00</currNotional>
<fixingDate>2016-06-24</fixingDate>
<intDays>42</intDays>
</tradeLeg>
问题是输出不是有效的 XML 文件,它缺少 XML 声明和根元素(XML 验证正在抱怨The markup in the document following the root element must be well-formed.
)
如何更改 XSLT 来解决此问题?
解决方案
要匹配 xslt 中的根节点,可以使用“/”。
它选择根节点,它是包含所有其他节点的文档节点。
此代码可以帮助实现相同的目的:
<xsl:template match="/">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
您可以如下修改 xslt 以添加根元素。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:hsbcfixml="http://www.fixprotocol.org/FIXML-4-4"
exclude-result-prefixes="hsbcfixml">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template
match="node()[not(self::tradeLeg or ancestor-or-self::assetId or ancestor-or-self::index or ancestor-or-self::basis1 or ancestor-or-self::rate or ancestor-or-self::spread or ancestor-or-self::pors or ancestor-or-self::matDate or ancestor-or-self::notional or ancestor-or-self::currNotional or ancestor-or-self::fixingDate or ancestor-or-self::intDays)]">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
在此处查看演示:https ://xsltfiddle.liberty-development.net/bnnZWv
如果我遗漏了什么,请告诉我。
推荐阅读
- javascript - 在 JavaScript 中将圆转换为折线
- python - 如何在两个 forloops 中获取 forloop.counter
- docker - docker-compose 启动单个容器
- python - 将特定公式应用于熊猫数据框
- html - 如何在高度为 100Vh 的页面上添加 100vh 的标题图像?
- windows - PowerShell中的管道:不将参数绑定到参数
因为它是空的 - c++ - 给定一个非空整数数组 nums,每个元素出现两次,除了一个。使用散列找到那个单一的
- php - 未捕获的错误:调用未定义的方法 mysqli_stmt::fetchAll()
- css - NextJS 的自定义 CSS 支持
- reactjs - 反应路由器 CSSTransition