'HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. | When I copy-of result of set:distinct

StackOverflow https://stackoverflow.com/questions/12640039

  •  04-07-2021
  •  | 
  •  

Вопрос

I trying transform (in eclipse) a document below:

<doc>
   <city name="Paris"
         country="France" />
   <city name="Madrid"
         country="Spain" />
   <city name="Vienna"
         country="Austria" />
   <city name="Barcelona"
         country="Spain" />
   <city name="Salzburg"
         country="Austria" />
   <city name="Bonn"
         country="Germany" />
   <city name="Lyon"
         country="France" />
   <city name="Hannover"
         country="Germany" />
   <city name="Calais"
         country="France" />
   <city name="Berlin"
         country="Germany" />
</doc>

with xslt:

<xsl:template match="/">
   <out>
      <all-countries>
            <xsl:copy-of select="//city" />
      </all-countries>
      <distinct-countries>
            <xsl:copy-of select="set:distinct(//@country/..)" />
      </distinct-countries>
   </out>
</xsl:template>

I'm use Xalan 2.7.1 it's work fine, but when I'm use 'JRE Instance Default' processor I get error:

16:07:20,642 ERROR [main] Main  - java.lang.RuntimeException: Run-time internal error in 'HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. '
Это было полезно?

Решение

This is guessing, but perhaps you can try other ways of getting the distinct values. Here are two suggestions, both of which are XSLT2.0 solutions:

<distinct-countries>
    <xsl:copy-of select="distinct-values(//@country)" /> 
</distinct-countries>

<xsl:for-each-group select="//city" group-by="@country">
  <xsl:value-of select="current-grouping-key()" />
</xsl:for-each-group>

If you are using XSLT1.0, the way to do this without using an extension function, it to make use of a technique called Muenchian Grouping. Firstly define a key to 'group' city elements by their country attribute.

<xsl:key name="countries" match="city" use="@country" />

Then you can pick out the distinct countries, by selecting the first city elements that occur in each group.

<distinct-countries> 
   <xsl:for-each select="//city[generate-id() = generate-id(key('countries', @country)[1])]"> 
      <xsl:value-of select="@country" />
   </xsl:for-each>
</distinct-countries> 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top