【问题标题】:'HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. | When I copy-of result of set:distinct'HIERARCHY_REQUEST_ERR:试图在不允许的地方插入节点。 |当我复制 set:distinct 的结果时
【发布时间】:2012-09-20 08:10:07
【问题描述】:

我尝试转换(在 Eclipse 中)以下文档:

<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>

使用 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>

我使用的是 Xalan 2.7.1,它工作正常,但是当我使用 'JRE Instance Default' 处理器时出现错误:

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. '

【问题讨论】:

    标签: java eclipse xslt xalan


    【解决方案1】:

    这是猜测,但也许您可以尝试其他方法来获取不同的值。这里有两个建议,都是XSLT2.0的解决方案:

    <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>
    

    如果您使用的是 XSLT1.0,那么在不使用扩展函数的情况下执行此操作的方法是使用一种称为 Muenchian Grouping 的技术。首先通过国家属性定义一个“分组”城市元素的键。

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

    然后您可以通过选择每个组中出现的第一个城市元素来挑选不同的国家/地区。

    <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> 
    

    【讨论】:

    • 据我了解 xsl:for-each-group 仅在 XSLT 2.0 中可用。对吗?
    • 啊,是的,这些是 XSLT2.0 解决方案。如果您只有 XSLT1.0,则可以使用一种称为 Muenchian Grouping 的技术。如果是这种情况,我可以扩展我的答案以显示这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多