XSL 从多个 XML 字段创建新元素,包括一些具有相同名称的字段
XSL Create New Element from MultipleXML Fields including some with the Same Name
我正在尝试通过将多个元素(一些具有相同名称)连接到一个新元素中来创建一个新的 XML 元素。我可以通过加入 Publisher 元素来创建新元素,但我不确定如何继续。最终结果将是一个新的 |包含键、作者、标题和出版商的定界元素。 multi-valued 字段将以逗号分隔。我可以使用 XSL 1.0 或 2.0。谢谢
输入文件
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
</Record>
</Records>
期望的输出
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
<Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
<Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
</Record>
</Records>
到目前为止的代码
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:copy-of select="Publisher"/>
<xsl:apply-templates select="*[not(self::Publisher)]" />
<Combined>
<xsl:apply-templates select="Publisher/text()" />
</Combined>
</xsl:copy>
</xsl:template>
<xsl:template match="Publisher/text()">
<xsl:if test="position() > 1">|</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
将您的 Record
模板更改为以下内容并省略最后一个模板 <xsl:template match="Publisher/text()">
。
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
<Combined>
<xsl:for-each select="*[not(self::Entrydate)]">
<xsl:choose>
<xsl:when test="name() = name(preceding-sibling::*[1])">, </xsl:when>
<xsl:when test="position() = 1" />
<xsl:otherwise>|</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." />
</xsl:for-each>
</Combined>
</xsl:copy>
</xsl:template>
结果如愿:
<?xml version="1.0"?>
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
<Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
<Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
</Record>
</Records>
我会做:
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates/>
<Combined>
<xsl:value-of select="Key, string-join(Author, ','), string-join(Title, ','), string-join(Publisher, ',')" separator="|"/>
</Combined>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
P.S。应该注意的是,这里有一个潜在的假设,即 none 的连接值将永远包含 ,
或 |
定界字符。这似乎是一个非常大胆的假设——尤其是。关于出版物标题中的逗号。
我正在尝试通过将多个元素(一些具有相同名称)连接到一个新元素中来创建一个新的 XML 元素。我可以通过加入 Publisher 元素来创建新元素,但我不确定如何继续。最终结果将是一个新的 |包含键、作者、标题和出版商的定界元素。 multi-valued 字段将以逗号分隔。我可以使用 XSL 1.0 或 2.0。谢谢
输入文件
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
</Record>
</Records>
期望的输出
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
<Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
<Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
</Record>
</Records>
到目前为止的代码
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:copy-of select="Publisher"/>
<xsl:apply-templates select="*[not(self::Publisher)]" />
<Combined>
<xsl:apply-templates select="Publisher/text()" />
</Combined>
</xsl:copy>
</xsl:template>
<xsl:template match="Publisher/text()">
<xsl:if test="position() > 1">|</xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
将您的 Record
模板更改为以下内容并省略最后一个模板 <xsl:template match="Publisher/text()">
。
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
<Combined>
<xsl:for-each select="*[not(self::Entrydate)]">
<xsl:choose>
<xsl:when test="name() = name(preceding-sibling::*[1])">, </xsl:when>
<xsl:when test="position() = 1" />
<xsl:otherwise>|</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." />
</xsl:for-each>
</Combined>
</xsl:copy>
</xsl:template>
结果如愿:
<?xml version="1.0"?>
<Records>
<Record>
<Key>11111</Key>
<Author>AA</Author>
<Author>AB</Author>
<Author>AC</Author>
<Author>AD</Author>
<Author>AE</Author>
<Entrydate>10/12/1956</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Combined>11111|AA, AB, AC, AD, AE|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5</Combined>
</Record>
<Record>
<Key>33333</Key>
<Author>BA</Author>
<Author>BB</Author>
<Entrydate>10/12/1965</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Combined>33333|BA, BB|Paper Title 1, Paper Title 2|Publisher 1, Publisher 2</Combined>
</Record>
<Record>
<Key>22222</Key>
<Author>CA</Author>
<Entrydate>11/12/1966</Entrydate>
<Title>Paper Title 1</Title>
<Publisher>Publisher 1</Publisher>
<Combined>22222|CA|Paper Title 1|Publisher 1</Combined>
</Record>
<Record>
<Key>44444</Key>
<Author>DA</Author>
<Author>DB</Author>
<Author>DC</Author>
<Author>DD</Author>
<Author>DE</Author>
<Author>DF</Author>
<Entrydate>09/12/1976</Entrydate>
<Title>Paper Title 1</Title>
<Title>Paper Title 2</Title>
<Title>Paper Title 3</Title>
<Title>Paper Title 4</Title>
<Title>Paper Title 5</Title>
<Title>Paper Title 6</Title>
<Publisher>Publisher 1</Publisher>
<Publisher>Publisher 2</Publisher>
<Publisher>Publisher 3</Publisher>
<Publisher>Publisher 4</Publisher>
<Publisher>Publisher 5</Publisher>
<Publisher>Publisher 6</Publisher>
<Combined>44444|DA, DB, DC, DD, DE, DF|Paper Title 1, Paper Title 2, Paper Title 3, Paper Title 4, Paper Title 5, Paper Title 6|Publisher 1, Publisher 2, Publisher 3, Publisher 4, Publisher 5, Publisher 6</Combined>
</Record>
</Records>
我会做:
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates/>
<Combined>
<xsl:value-of select="Key, string-join(Author, ','), string-join(Title, ','), string-join(Publisher, ',')" separator="|"/>
</Combined>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
P.S。应该注意的是,这里有一个潜在的假设,即 none 的连接值将永远包含 ,
或 |
定界字符。这似乎是一个非常大胆的假设——尤其是。关于出版物标题中的逗号。