【发布时间】:2017-11-04 19:43:11
【问题描述】:
如果属性值匹配,我一直在尝试合并 books.xml 中的所有文本节点。并且这些值应该用任何分隔符分隔(例如:等)
这里是源 XML:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk101">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
<book id="bk102">
<author>Corets, Eva</author>
<title>The Sundered Grail</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
<description>The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk101">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
这是所需的输出:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew Galos, Mike Corets, Eva</author>
<title>XML Developer's Guide Visual Studio 7: A Comprehensive Guide Oberon's Legacy</title>
<genre>Computer Computer</genre>
<price>44.95 49.95 5.95</price>
<publish_date>2000-10-01 2001-04-16 2001-03-10</publish_date>
<description>An in-depth look at creating applications
with XML. Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment. In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
<book id="bk102">
<author>Ralls, Kim Corets, Eva</author>
<title>Midnight Rain The Sundered Grail</title>
<genre>Fantasy Fantasy</genre>
<price>5.95 5.95</price>
<publish_date>2000-12-16 2001-09-10</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world. The two daughters of Maeve, half-sisters,
battle one another for control of England. Sequel to
Oberon's Legacy.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
</catalog>
这是我一直在尝试的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="Dbook" match="book" use="@id"/>
<xsl:template match="/catalog">
<xsl:copy>
<xsl:for-each select="book[count(. | key('Dbook', @id)[1])=1]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="key('Dbook',@id)">
<xsl:variable name="author" select="author"/>
<xsl:variable name="title" select="title"/>
<xsl:variable name="genre" select="genre"/>
<xsl:variable name="price" select="price"/>
<xsl:variable name="publish_date" select="publish_date"/>
<xsl:variable name="description" select="description"/>
<xsl:variable name="null"/>
<xsl:copy-of select="concat($author,$null)"/>
<xsl:copy-of select="concat($title,$null)"/>
<xsl:copy-of select="concat($genre,$null)"/>
<xsl:copy-of select="concat($price,$null)"/>
<xsl:copy-of select="concat($publish_date,$null)"/>
<xsl:copy-of select="concat($description,$null)"/>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
请选择 XSLT 1.0 或 2.0,而不是两者都选 - 尤其是。当您询问分组时。在 XSLT 2.0 中,使用
xsl:for-each-group可以轻松完成分组。使用带有 separator` 属性的xsl:value-of合并文本节点同样简单。 -- 请注意,无法生成您显示的输出,因为 XML 中不允许使用未转义的&字符。 -
这是一个与您之前的问题非常相似的问题,因此也许您应该从那里接受答案开始,然后尝试使其适应您的新要求,并在遇到困难时向我们展示您的尝试。顺便说一句,如果您想要的输出应该是 XML,那么分隔符需要转义为
<author>Gambardella, Matthew &amp; Galos, Mike &amp; Corets, Eva</author> -
@michael.hor257k 我想用 XSLT 1.0 实现它,即使我在没有未转义 & 字符的情况下实现它也可以
标签: xml xslt xslt-1.0 xslt-2.0 xslt-grouping