【问题标题】:XSTL filter with element content带有元素内容的 XSLT 过滤器
【发布时间】:2013-06-27 15:01:16
【问题描述】:

我正在尝试优化 xsl,因为修改我的数据需要很长时间。

我的源数据(我无法修改)如下所示:

<catalog>
    <product>
        <prodid>12345</prodid>
        .....
    </proudct>
    <product_group_map>
       <prodid>12345</prodid>
       <groupid>2435</groupid>
    </product_group_map>
</catalog>

我想在产品中使用这些 groupId。我到目前为止所做的看起来是这样的:

<xsl:variable name="id"><xsl:value-of select="prodid"/></xsl:variable>

<xsl:variable name="grId">
    <xsl:for-each select="../product_group_map">
        <xsl:if test="prodid = $id">
            <xsl:value-of select="groupid"/>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

<!-- the actual print of the values -->
<xsl:value-of select="concat( $id, $separator, $grId)"/>

所以进程运行 O^2。对于 700 万种产品来说,这是不行的。有没有办法以其他方式匹配所需的 groupId ? 我想到了类似的东西:如果 xml 看起来像这样

<catalog>
    <product>
        <prodid>12345</prodid>
        .....
    </proudct>
    <product_group_map prodId="12345">
       <groupid>2435</groupid>
    </product_group_map>
</catalog>

我可以像这样使用选择:

<!--allready in product with the path -->
<xsl:variable name="id"><xsl:value-of select="prodid"/></xsl:variable>
<xsl:variable name="groupid"><xsl:value-of select="../product_group_map[@prodid = $prodId]/groupid"/></xsl:variable>

【问题讨论】:

    标签: xml xslt xpath xml-parsing


    【解决方案1】:

    首先,让xsl:variable 只包含value-of 是低效的,最好将选择表达式放在xsl:variable 本身上。其次,您可以使用当前设置对属性执行与您建议的相同的操作,其中 prodid 是一个元素

    <xsl:variable name="groupid" select="../product_group_map[prodid = $id]/groupid" />
    

    但是定义一个(在任何模板之外)可能更有效

    <xsl:key name="groupByProduct" match="prod_group_map/groupid" use="../prodid" />
    

    然后您可以简单地使用找到组 ID

    <xsl:variable name="groupid" select="key('groupByProduct', $id)" />
    

    请注意,如果相同的prodid 由不同的product_group_map 元素链接到多个不同的组ID,则生成的$groupid 变量将是一个包含所有匹配groupid 元素的节点集。

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多