xslt删除重复的元素

时间:2015-12-04 12:09:30

标签: xml xslt-2.0 saxon

我正在尝试从此文件中删除具有相同href属性的元素:

this.tableLayoutPanel1.GetControlFromPosition(column, row).BackColor = Color.Red;

使用此xslt 2.0样式表

  CalendarView _calendarView;
  _calendarView = new CalendarView ();
                    _calendarView.MinDate = DateTime.Parse ("01/01/1700");
                    _calendarView.MaxDate = DateTime.Parse ("12/31/2025");
                    _calendarView.DateSelected += _calendarView_DateSelected;
                    _calendarView.SelectedDate=Convert.ToDateTime("12/10/2015 12:00:00 AM");
                    _calendarView.BackgroundColor=Color.FromRgb(72,110,195);
                    _calendarView.SelectedDateBackgroundColor=Color.Red;
                    _calendarView.SelectedDateForegroundColor=Color.White;

此处的预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<images>
    <item id="d1e152_1" href="Pic/104764.jpg" media-type="image/jpeg"/>
    <item id="d1e163_2" href="Pic/104764.jpg" media-type="image/jpeg"/>
    <item id="d1e174_3" href="Pic/104764.jpg" media-type="image/jpeg"/>
    <item id="d1e218_4" href="Pic/104763.jpg" media-type="image/jpeg"/>
    <item id="d1e349_5" href="Pic/110001.tif" media-type="image/tif"/>
    <item id="d1e681_6" href="Pic/199201.tif" media-type="image/tif"/>
    <item id="d1e688_7" href="Pic/124566.tif" media-type="image/tif"/>
</images>

为什么它不起作用?我仍然在生成的xsl文件中包含所有重复项。

2 个答案:

答案 0 :(得分:1)

我明白了......一直发生在我身上......把每个小组放在/ images的模板中......

更新了XSLT 2.0 ......

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">

  <xsl:output method="xml"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="/images">
    <new_images>
      <xsl:for-each-group select="item" group-by="@href">
        <xsl:copy-of select="current-group()[1]"/>
      </xsl:for-each-group>
    </new_images>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

此XSLT 1.0解决方案更短且不慢:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kByHref" match="item" use="@href"/>

  <xsl:template match="/*">
    <new_images>
      <xsl:copy-of select=
      "*[generate-id() = generate-id(key('kByHref', @href)[1])]"/>
    </new_images>
  </xsl:template>
</xsl:stylesheet>