如何在for-each循环中获取XSLT中兄弟节点的值

时间:2015-10-02 09:37:13

标签: xslt

我试图在另一个兄弟节点的不同值的for-each循环中获取存储在兄弟节点中的值:

我的XML:

<LaunchedMeterClass id="584e348b-2a06-42d0-a858-b8909f579238-St-4M-Template-Standard-cF">
    <property key="ClusterContractUUID" value="c2cebd90-9265-4cea-8018-0aac6efcced2"/>
    <property key="OVClustered" value="true"/>
</LaunchedMeterClass>
<LaunchedMeterClass id="584e348b-2a06-42d0-a858-b8909f579238-St-4M-Template-Standard-cE">
    <property key="ClusterContractUUID" value="c2cebd90-9265-4cea-8018-0aac6efcced2"/>
    <property key="OVClustered" value="true"/>
</LaunchedMeterClass>
<LaunchedMeterClass id="584e348b-2a06-42d0-a858-b8909f579238-St-4M-Template-Standard-cC">
    <property key="ClusterContractUUID" value="d0c9f440-172c-49ad-9b95-cddce23f16fa"/>
    <property key="OVClustered" value="false"/>
</LaunchedMeterClass>
<LaunchedMeterClass id="584e348b-2a06-42d0-a858-b8909f579238-St-4M-Template-Standard-cD">
    <property key="ClusterContractUUID" value="d0c9f440-172c-49ad-9b95-cddce23f16fa"/>
    <property key="OVClustered" value="false"/>
</LaunchedMeterClass>

我的XSLT(不起作用):

<xsl:choose>
<xsl:when test="//LaunchedMeterClass/property/@key='ClusterContractUUID'">
    <xsl:for-each select="distinct-values(descendant::property[@key='ClusterContractUUID']/@value)">
        <contract-type>
            <property>
                <xsl:attribute name="key">ClusterContractUUID</xsl:attribute>
                <xsl:attribute name="value">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </property>
            <property>
                <xsl:attribute name="key">OVClustered</xsl:attribute>
                <xsl:attribute name="value">
                    <xsl:value-of select="following-sibling::*[1]/property[@key='OVClustered']/@value"/>
                </xsl:attribute>
            </property>
        </contract-type>
    </xsl:for-each>
</xsl:when>
<xsl:otherwise>
    <contract-type>
        <property>
            <xsl:attribute name="key">contractTypeCategory</xsl:attribute>
            <xsl:attribute name="value">service</xsl:attribute>
        </property>
    </contract-type>
</xsl:otherwise>

我想要的结果:

<contract-type>
    <property key="ClusterContractUUID" value="c2cebd90-9265-4cea-8018-0aac6efcced2"/>
    <property key="OVClustered" value="true"/>
</contract-type>
<contract-type>
    <property key="ClusterContractUUID" value="d0c9f440-172c-49ad-9b95-cddce23f16fa"/>
    <property key="OVClustered" value="false"/>
</contract-type>

我也尝试过ancestor ::,descendant ::和preceding-sibling :: etc.但是我总是遇到这个错误:以下兄弟轴的上下文项的必需项类型是node();提供的值具有项类型xs:anyAtomicType。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

distinct-values为您提供一系列字符串(或一般的原始值),而不是一系列节点。原始值没有兄弟姐妹。如果要对节点进行分组,则需要使用xsl:for-each-group

<xsl:for-each-group select="LaunchedMeterClass" group-by="property[@key='ClusterContractUUID']/@value">
  <contract-type>...</contract-type>
</xsl:for-each-group>

我认为,要复制property元素,您只需要

<xsl:template match="root">
  <xsl:for-each-group select="LaunchedMeterClass" group-by="property[@key='ClusterContractUUID']/@value">
    <contract-type>
        <xsl:copy-of select="property"/>
    </contract-type>
  </xsl:for-each-group>    
</xsl:template>