0

我有一组通用的标签,需要用不同的包装元素包装。示例输入 XML 就像 -

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<a>Hello there</a>
<code>FJ-123-99</code>
<isPopular>True</isPopular>
<timestamp>2019-10-17 07:57:23</timestamp>
  <pop>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <about>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </about>
  </pop>
  <classic>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <about>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </about>
  </classic>
  <retro>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <about>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </about>
  </retro>
</catalog>

样本输出

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<a type="primary">Hello there</a>
<typeCode>FJ12399</typeCode>
<isPopular>Y</isPopular>
<timestamp>20191017:075723</timestamp>
  <pop>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <about>
    <organization>Columbia</organization>
    <amount>10.90</amount>
    <releaseTime>1985</releaseTime>
    </about>
  </pop>
  <classic>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <about>
    <organization>CBS Records</organization>
    <amount>9.90</amount>
    <releaseTime>1988</releaseTime>
    </about>
  </classic>
  <retro>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <about>
    <organization>USA</organization>
    <amount>9.90</amount>
    <releaseTime>1982</releaseTime>
    </about>
  </retro>
</catalog>

在这里,<title> <artist>和对于像和<about>之类的主要包装器很常见。<pop> <retro><classic>

我的问题是如何将这些通用模板应用到我的主要包装器中,我能够进行所有转换,例如转换时间戳、删除连字符、更改标签名称。

4

1 回答 1

0

从作为基本模板的身份转换开始,即

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

仅此一项就可以逐级递归地复制所有内容,但是如果您通常为要转换的元素或节点添加自己的模板,则应该得到您想要的,即在您的情况下复制/保留例如mathBooksenglishBook或者scienceBook让您的模板用于例如address,或author为他们所做的小改变而努力;编辑后,您似乎想要更改catalog/a元素,例如添加属性,因此从上面显示的模板开始并添加例如

<xsl:template match="catalog/a">
  <a type="primary">
    <xsl:apply-templates/>
  </a>
</xsl:template>

使用这种方法,您可以为需要应用到不同节点或元素的任何进一步更改添加模板。

于 2021-04-01T10:04:52.100 回答