我不能完全肯定這回答你正在尋找,但在這裏我會給你兩個例子。其中一個應該可能適合您的需求。
我用下面的輸入XML:
<?xml version="1.0" encoding="UTF-8"?>
<Events>
<Properties>
<Property Descriptor="100">1378314022</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314023</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314024</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314022</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314023</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314024</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="100">MD2356</Property>
<Property Descriptor="200">25689</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">5632</Property>
</Properties>
</Events>
當我使用的下一個XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Properties">
<xsl:copy>
<xsl:apply-templates select="Property[@Descriptor = '100'][1]" />
<xsl:apply-templates select="Property[@Descriptor = '100'][last()]" />
<xsl:apply-templates select="Property[@Descriptor = '200'][1]" />
<xsl:apply-templates select="Property[@Descriptor = '200'][last()]" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
這XSLT也有過Properties
元素的迭代,即Properties
元素中它得到的第一次和最後一次發生的每個提供Descriptor
。其結果將是:
<?xml version="1.0" encoding="UTF-8"?>
<Events>
<Properties>
<Property Descriptor="100">1378314022</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314023</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314024</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314022</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314023</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="200">5632</Property>
</Properties>
<Properties>
<Property Descriptor="100">1378314024</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">123456</Property>
<Property Descriptor="200">5632</Property>
</Properties>
</Events>
如果你想獲得最後的恩在所有Properties
的XSLT第一次出現應該是略有不同:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Events">
<xsl:copy>
<Properties>
<xsl:apply-templates select="(Properties/Property[@Descriptor = '100'])[1]" />
<xsl:apply-templates select="(Properties/Property[@Descriptor = '100'])[last()]" />
<xsl:apply-templates select="(Properties/Property[@Descriptor = '200'])[1]" />
<xsl:apply-templates select="(Properties/Property[@Descriptor = '200'])[last()]" />
</Properties>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
結果將被:
<?xml version="1.0" encoding="UTF-8"?>
<Events>
<Properties>
<Property Descriptor="100">1378314022</Property>
<Property Descriptor="100">MD75632</Property>
<Property Descriptor="200">ABC1234</Property>
<Property Descriptor="200">5632</Property>
</Properties>
</Events>
感謝您的優雅的解決方案和詳細的解釋。我已經應用*** Events/Properties [屬性[@描述符=「200」])返回(先前 - 兄弟::屬性[1] [屬性[@描述符=「200」] = $ me],以下 - 兄弟::屬性[1] [屬性[@描述=「200」] = $我]))!= 2] ***到我的應用程序,但迭代似乎沒有工作。事件之前有節點,但我將它們添加到迭代中。我可以做嗎? –
我也試過*** // Events/Properties [count(for $ me in(Property [@ Descriptor =「200」])return(preceding-sibling :: Properties [1] [Property [@ Descriptor =「200」 ] = $ me],以下 - 兄弟::屬性[1] [屬性[@描述=「200」] = $我]))!= 2] ***,它沒有工作。 :( –
@Dalek「沒有工作」 - 你得到了什麼輸出?你得到的比預期的還要多,或者你什麼都沒有得到?真正的文檔是否包含任何命名空間,就好像你需要在你的XPath表達式中,通過綁定適當的前綴來考慮到這一點,具體怎麼做取決於你使用的是什麼工具/庫/編程語言,你在問題中沒有說明。 –