【问题标题】:XSL select distinct child node and get all the parents group by those childXSL 选择不同的子节点并按这些子节点获取所有父组
【发布时间】:2016-10-20 18:33:13
【问题描述】:

我有一个看起来像这样的 xml:

<texto id="1">
    <user_id>1</user_id>
    ...
</texto>
<texto id="2">
    <user_id>1</user_id>
    ...
</texto>
<texto id="3">
    <user_id>2</user_id>
    ...
</texto>

我想把它改成这样:

<user id="1">
    <texto id="1">...</texto>
    <texto id="2">...</texto>
</user>
<user id="2">
    <texto id="3">...</texto>
</user>

我尝试以下 xsl:

    <xsl:template match="/">
        <xsl:variable name="user_ids" select="distinct-values(//document/texto/user_id)"/>
        <xsl:for-each select="$user_ids">
            <xsl:variable name="user_id" select="."/>
            <xsl:variable name="textos" select="//document/texto[user_id = $user_id]"/>
            <user id="$user_id">

            </user>
        </xsl:for-each>
    </xsl:template>

但此时我得到一个错误:前导'/'无法选择包含上下文项的树的根节点:上下文项不是节点

如何按不同的用户子节点对所有 texto 节点进行分组?

谢谢

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    使用

    <xsl:for-each-group select="//texto" group-by="user_id"> <user id="{current-grouping-key()}"> <xsl:apply-templates select="current-group()"/> </user> </xsl:for-each-group>

    然后为 texto 元素编写一个模板

    <xsl:template match="texto"> <xsl:copy> <xsl:copy-of select="@*, node() except user_id"/> </xsl:copy> </xsl:template>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多