使用XSLT在组中显示第一个不同的XML节点

时间:2015-06-23 21:19:39

标签: xml xslt-1.0

我需要通过XSLT运行数据以使元素正确排序。但是,只要同一父级中有两个或多个匹配字段,我只希望显示不同的字段数据。在以下XML中,您将看到两个重复的<Scott>个数字(3090&amp; 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> 

1 个答案:

答案 0 :(得分:0)

只需替换

<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)] - 否则