如何使用 xslt 添加根元素?

l0r3nz4cc10

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

如果我遗漏了什么,请告诉我。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章