使用 XSLT 在组中显示第一个不同的 XML 节点
Display 1st distinct XML node in group using XSLT
我需要通过 XSLT 运行 数据以正确排列元素。但我只想在同一父项中有两个或多个匹配字段时显示不同的字段数据。在下面的 XML 中,您将看到两个重复的 <Scott>
号码(3090 和 3137)。只应显示第一个 <Scott>
字段。
但是,我想保留空标签,或者在空槽中插入一个全新的标签,以便留出间距。在某些情况下,<Scott>
数字可能会在一个数字中出现两次或更多次 <Group>
这可能吗?
这是示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<stamps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Group>
<stamp_image>3090.jpg</stamp_image>
<Date>08/07/96</Date>
<stamp>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott>3090</Scott>
<Title>Pane of 20</Title>
</stamp>
</Group>
<Group>
<stamp_image>3096.jpg</stamp_image>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<stamp>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
</stamp>
<stamp>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</stamp>
</Group>
<Group>
<stamp_image>3137.jpg</stamp_image>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<stamp>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott>3137</Scott>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
</stamp>
<stamp>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</stamp>
</Group>
</stamps>
这是当前输出:
<?xml version="1.0" encoding="utf-8"?>
<stamps>
<Group>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott>3090</Scott>
<Title>Pane of 20</Title>
</Group>
<Group>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</Group>
<Group>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott>3137</Scott>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</Group>
</stamps>
这是所需的输出
<?xml version="1.0" encoding="utf-8"?>
<stamps>
<Group>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott/>
<Title>Pane of 20</Title>
</Group>
<Group>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</Group>
<Group>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott/>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</Group>
</stamps>
这是当前的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<stamps><xsl:apply-templates select="stamps"/></stamps>
</xsl:template>
<xsl:template match="stamps">
<xsl:apply-templates select="Group"/>
</xsl:template>
<xsl:template match="Group">
<Group>
<xsl:apply-templates select="Heading"/>
<xsl:apply-templates select="HeadingDate"/> <xsl:apply-templates select="stamp"/></Group></xsl:template>
<xsl:template match="Heading"><xsl:text>
</xsl:text><Heading><xsl:value-of select="."/></Heading></xsl:template>
<xsl:template match="HeadingDate"><HeadingDate><xsl:value-of select="."/> </HeadingDate></xsl:template>
<xsl:template match="Scott">
<Scott><xsl:value-of select="."/></Scott></xsl:template>
<xsl:template match="Minor">
<Minor><xsl:value-of select="."/></Minor></xsl:template>
<xsl:template match="Title">
<Title><xsl:value-of select="."/></Title><xsl:text>
</xsl:text></xsl:template>
<xsl:template match="Date">
<Date><xsl:value-of select="."/></Date>
</xsl:template>
</xsl:stylesheet>
只需更换
<xsl:template match="Scott">
<Scott><xsl:value-of select="."/></Scott>
</xsl:template>
和
<xsl:template match="Scott[not(text() = preceding-sibling::Scott)]">
<Scott><xsl:value-of select="."/></Scott>
</xsl:template>
<xsl:template match="Scott[text() = preceding-sibling::Scott]">
<Scott/>
</xsl:template>
Scott[text() = preceding-sibling::Scott] - 它匹配所有具有至少一个具有相同文本内容的前 Scott 兄弟的所有 Scott 元素
Scott[not(text() = preceding-sibling::Scott)] - 否则
我需要通过 XSLT 运行 数据以正确排列元素。但我只想在同一父项中有两个或多个匹配字段时显示不同的字段数据。在下面的 XML 中,您将看到两个重复的 <Scott>
号码(3090 和 3137)。只应显示第一个 <Scott>
字段。
但是,我想保留空标签,或者在空槽中插入一个全新的标签,以便留出间距。在某些情况下,<Scott>
数字可能会在一个数字中出现两次或更多次 <Group>
这可能吗?
这是示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<stamps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Group>
<stamp_image>3090.jpg</stamp_image>
<Date>08/07/96</Date>
<stamp>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott>3090</Scott>
<Title>Pane of 20</Title>
</stamp>
</Group>
<Group>
<stamp_image>3096.jpg</stamp_image>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<stamp>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
</stamp>
<stamp>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</stamp>
</Group>
<Group>
<stamp_image>3137.jpg</stamp_image>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<stamp>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott>3137</Scott>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
</stamp>
<stamp>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</stamp>
</Group>
</stamps>
这是当前输出:
<?xml version="1.0" encoding="utf-8"?>
<stamps>
<Group>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott>3090</Scott>
<Title>Pane of 20</Title>
</Group>
<Group>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</Group>
<Group>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott>3137</Scott>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</Group>
</stamps>
这是所需的输出
<?xml version="1.0" encoding="utf-8"?>
<stamps>
<Group>
<Scott>3090</Scott>
<Title>32¢ Rural Free Delivery</Title>
<Scott/>
<Title>Pane of 20</Title>
</Group>
<Group>
<Heading>Legends of American Music: Big Band Leaders</Heading>
<HeadingDate>09/11/96</HeadingDate>
<Scott>3099</Scott>
<Title>32¢ Benny Goodman</Title>
<Minor>a</Minor>
<Title>Block or strip of 4, #3100-3103</Title>
</Group>
<Group>
<Heading>Looney Tunes</Heading>
<HeadingDate>05/22/97</HeadingDate>
<Scott>3137</Scott>
<Title>Pane of 10</Title>
<Scott/>
<Title>Die-cutting on #3137b does not extend through the backing paper.</Title>
<Minor>a</Minor>
<Title>32¢ Bugs Bunny</Title>
</Group>
</stamps>
这是当前的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<stamps><xsl:apply-templates select="stamps"/></stamps>
</xsl:template>
<xsl:template match="stamps">
<xsl:apply-templates select="Group"/>
</xsl:template>
<xsl:template match="Group">
<Group>
<xsl:apply-templates select="Heading"/>
<xsl:apply-templates select="HeadingDate"/> <xsl:apply-templates select="stamp"/></Group></xsl:template>
<xsl:template match="Heading"><xsl:text>
</xsl:text><Heading><xsl:value-of select="."/></Heading></xsl:template>
<xsl:template match="HeadingDate"><HeadingDate><xsl:value-of select="."/> </HeadingDate></xsl:template>
<xsl:template match="Scott">
<Scott><xsl:value-of select="."/></Scott></xsl:template>
<xsl:template match="Minor">
<Minor><xsl:value-of select="."/></Minor></xsl:template>
<xsl:template match="Title">
<Title><xsl:value-of select="."/></Title><xsl:text>
</xsl:text></xsl:template>
<xsl:template match="Date">
<Date><xsl:value-of select="."/></Date>
</xsl:template>
</xsl:stylesheet>
只需更换
<xsl:template match="Scott">
<Scott><xsl:value-of select="."/></Scott>
</xsl:template>
和
<xsl:template match="Scott[not(text() = preceding-sibling::Scott)]">
<Scott><xsl:value-of select="."/></Scott>
</xsl:template>
<xsl:template match="Scott[text() = preceding-sibling::Scott]">
<Scott/>
</xsl:template>
Scott[text() = preceding-sibling::Scott] - 它匹配所有具有至少一个具有相同文本内容的前 Scott 兄弟的所有 Scott 元素
Scott[not(text() = preceding-sibling::Scott)] - 否则