Unique attribute based xml nodes

I want to use XSLT to convert a collection of documents into one structure. I have the correct conversion to combine documents. However, I do not know if the documents in them have duplicate entries that I will need to delete.

I need to know how to remove these duplicates (if they exist) using the id attribute. All duplicates will have the same identifier. I know this will have something to do with the keys and generate-id functions.

<root>
    <item id="1001">A</item>
    <item id="1003">C</item>
    <item id="1004">D</item>
    <item id="1002">B</item>
    <item id="1001">A</item>
    <item id="1003">C</item>
    <item id="1004">D</item>
    <item id="1005">E</item>
</root>

I need an XSLT 1.0 conversion for the above, based on the following ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

                  

Also, can anyone explain how this works for me? Bit noob ...

Thanks in advance...

+3
source share
2

generate-id(), , generate-id: -

<xsl:key name="items" match="item" use="@id" />

<xsl:template match="root">
    <root>
        <xsl:copy-of select="item[count(key('items',@id)[1]|.)=1]" />
    </root>
</xsl:template>

, , id . key , .

, node -set | . , node |, .

: -

 key('items',@id)

, . : -

 key('items',@id)[1]

, ( , node).

, : -

 count(key('items',@id)[1]|.)=1

item node id.

, copy-of node, .

+3

generate-id() @AnthonyWJones. . , , .

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:key name="kItemById" match="item" use="@id" />

  <xsl:template match="root">
    <copy>
      <xsl:copy-of select="
        item[generate-id() = generate-id(key('kItemById', @id)[1])]
      " />
    </copy>
  </xsl:template>
</xsl:stylesheet>

:

item[generate-id() = generate-id(key('kItemById', @id)[1])]

: " <item> s, @id".

+3
source

Source: https://habr.com/ru/post/1732496/


All Articles