XSL子元素到父属性,部分deux:元素的顺序重要吗?

时间:2015-09-22 12:15:37

标签: xml xslt

因此,基于我之前的问题(XSLT transform child element to attribute in the parent),我想将子元素中的数据用作父元素中的属性。问题是当我在" poolColor"之前转换具有另一个子元素的元素时,poolColor不会以某种方式添加为属性。

工作:

<Exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <parent xsi:type="SinglePool">
        <poolColor colorType="Basecolor" colorKey="GN"/>
        <poolLength type="full_length" length="2"/>
    </parent>
</Exchange>

结果:

<Exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <parent xsi:type="SinglePool" basecolor="GN">
        <poolLength type="full_length" length="2" />
    </parent>
</Exchange>

不工作:

<Exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <parent xsi:type="SinglePool">
        <poolLength type="full_length" length="2" id="N7" />
        <poolColor colorType="Basecolor" colorKey="GN" id="N3" />
    </parent>
</Exchange>

结果:

<Exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <parent xsi:type="SinglePool">
        <poolLength type="full_length" length="2" id="N7" />
    </parent>
</Exchange>

我使用的XSL代码如下:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="no" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="poolColor">
        <xsl:attribute name='{translate(@colorType,"BC","bc")}'>
        <xsl:value-of select="@colorKey" />
    </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

如果有人能够了解为什么会发生这种情况,我会很高兴。

1 个答案:

答案 0 :(得分:1)

由于compile 'com.google.android.gms:play-services-gcm:7.8.0' 已添加到xsl:attribute,因此第二个示例中的

<poolength>无效。根据XSLT rec:

,这是一个错误
  

以下是所有错误:
  添加子项后,向元素添加属性   它;实现可以发出错误信号或忽略错误   属性。   
...

以下内容可行,因为在其他内容之前处理任何<parent>子项:

poolColor
相关问题