Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt XSL:在XSL 1.0中组合两个节点集_Xslt_Xslt 1.0_Xalan - Fatal编程技术网

Xslt XSL:在XSL 1.0中组合两个节点集

Xslt XSL:在XSL 1.0中组合两个节点集,xslt,xslt-1.0,xalan,Xslt,Xslt 1.0,Xalan,在此之前,我将先介绍一个事实,即我是XSLT新手(本例中为1.0),并且自己很少有机会解决这个问题。我有以下XML: <Root> <Info> <Feature>SEA</Feature> <Sequence>10</Sequence> <Value>Y</Value> </Info> <Info> <Feature>SEA<

在此之前,我将先介绍一个事实,即我是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>

大海
10
Y
大海
20
Y
电话
10
N
电话
20
Y
电话
35
Y
在序列相同的情况下,我需要评估所有等于SEA的特性和所有等于TEL的特性。输出将包括原始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>

大海
10
Y
N
大海
20
Y
Y

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

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

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

太完美了!非常感谢你!