【问题标题】:XSL: Combining Two NodeSets in XSL 1.0XSL:在 XSL 1.0 中组合两个节点集
【发布时间】:2013-06-09 02:03:09
【问题描述】:

我将在此之前说明我是 XSLT(在本例中为 1.0)的新手,并且我自己解决这个问题的运气很差。我有以下 XML:

<Root>
<Info>
    <Feature>SEA</Feature>
    <Sequence>10</Sequence>
    <Value>Y</Value>
</Info>
<Info>
    <Feature>SEA</Feature>
    <Sequence>20</Sequence>
    <Value>Y</Value>
</Info>
<Info>
    <Feature>TEL</Feature>
    <Sequence>10</Sequence>
    <Value>N</Value>
</Info>
<Info>
    <Feature>TEL</Feature>
    <Sequence>20</Sequence>
    <Value>Y</Value>
</Info>
<Info>
    <Feature>TEL</Feature>
    <Sequence>35</Sequence>
    <Value>Y</Value>
</Info>
</Root>

我需要根据序列相同的所有等于 TEL 的功能评估所有等于 SEA 的功能。输出将包括原始 SEA 值和 TEL 值。

输出请求是:

<Root>
<Info>
    <Feature>SEA</Feature>
    <Sequence>10</Sequence>
    <SEAValue>Y</SEAValue>
    <TELValue>N</TELValue>
</Info>
<Info>
    <Feature>SEA</Feature>
    <Sequence>20</Sequence>
    <SEAValue>Y</SEAValue>
    <TELValue>Y</TELValue>  
</Info>
</Root>

【问题讨论】:

    标签: xslt xslt-1.0 xalan


    【解决方案1】:

    在 XSLT 中进行连接的最简单和最有效的方法是使用键。声明一个密钥:

    <xsl:key name="k" match="Info[Feature='TEL']" use="Sequence"/>
    

    然后你就可以了

    <xsl:for-each select="Info[Feature='SEA']">
      <xsl:copy-of select="Feature"/>
      <xsl:copy-of select="Sequence"/>
      <SEAValue><xsl:value-of select="value"/></SEAValue>
      <TELValue><xsl:value-of select="key('k', Sequence)/Value"/></TELValue>
    </xsl:for-each>
    

    【讨论】:

    • 太完美了!非常感谢!
    【解决方案2】:

    您可以尝试这个基于密钥的解决方案:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" indent="yes"/>
        <xsl:key name="kSequence" match="Info" use="Sequence"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Info/Value" >
            <xsl:element name="{../Feature}Value" >
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
        </xsl:template>
    
        <xsl:template match="/*" >
            <xsl:copy>
                <xsl:for-each select="//Info[count( . | key('kSequence', Sequence)[1])=1]" >
                    <xsl:variable name="is" select="key('kSequence', current()/Sequence)" />
                    <xsl:if test="$is[Feature = 'SEA'] and $is[Feature = 'TEL']" >
                        <xsl:copy>
                            <xsl:apply-templates select="$is[Feature = 'SEA']/*" />
                            <xsl:apply-templates select="$is[Feature = 'TEL']/Value" />
                        </xsl:copy>
                    </xsl:if>
                </xsl:for-each>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

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