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



我需要通过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()=前面的同级::Scott〕-它匹配所有至少有一个前面的Scott同级具有相同文本内容的Scott元素

Scott[not(text()=前面的同级::Scott)]-否则

相关内容

  • 没有找到相关文章

最新更新