尝试在for-each循环中获取父属性值

时间:2013-08-28 19:41:20

标签: xslt

我拼凑了一个例程,该例程将根据给定元素中的某些属性值生成过滤器属性值列表。功能如下:

<xsl:template name="have_arch_attrib">

    <!-- We only add a filter attribute IF there is a arch, condition or security attribute-->
    <xsl:choose>
        <xsl:when test=".[@arch] | .[@condition] | .[@security]">
            <xsl:attribute name="filter">
                <xsl:for-each select="@arch | @condition | @security ">

                    <!-- Need to check and convert semis to commas-->
                    <xsl:variable name="temp_string" select="."/>
                    <xsl:variable name="rep_string">
                        <xsl:value-of select="replace($temp_string, ';', ',')"/>
                    </xsl:variable>
                    <xsl:value-of select="$rep_string"/>


                    <!--<xsl:value-of select="." />-->
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:attribute>
        </xsl:when>
    </xsl:choose>
</xsl:template>

但是,对于某些元素,我需要检查该元素的父元素的属性。所以我重写了上面的内容:

<xsl:template name="parent_has_arch_attrib">

    <!-- We only add a filter attribute IF there is a arch, condition or security attribute-->
    <xsl:choose>
        <xsl:when test="..[@arch] | ..[@condition] | ..[@security]">
            <xsl:attribute name="filter">
                <xsl:for-each select="..[@arch] | ..[@condition] | ..[@security] ">

                    <!-- Need to check and convert semis to commas-->
                    <xsl:variable name="temp_string" select="."/>
                    <xsl:variable name="rep_string">
                        <xsl:value-of select="replace($temp_string, ';', ',')"/>
                    </xsl:variable>
                    <xsl:value-of select="$rep_string"/>


                    <!--<xsl:value-of select="." />-->
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:attribute>
        </xsl:when>
    </xsl:choose>
</xsl:template>

我正在进入这个例程,但什么都没有出来。我认为问题是当我通过select =“。”分配temp_string时。我相信这是获得当前元素。如果我尝试select =“..”,它将为我提供所有属性值,而不仅仅是for-each循环正在处理的当前属性值。我可以在for-each循环中做这样的事情,还是我必须将其制动出来?

感谢您的帮助!

拉​​斯

1 个答案:

答案 0 :(得分:0)

我认为你需要更换这一行......

<xsl:for-each select="..[@arch] | ..[@condition] | ..[@security] ">

改为使用此行

<xsl:for-each select="../@arch | ../@condition | ../@security ">

执行..[@arch] | ..[@condition] | ..[@security]时,您所做的就是选择父节点,如果存在其中一个指定属性,那么当您真正尝试获取属性时。

顺便说一句,你真的不需要在这里使用变量......

                <xsl:variable name="temp_string" select="."/>
                <xsl:variable name="rep_string">
                    <xsl:value-of select="replace($temp_string, ';', ',')"/>
                </xsl:variable>
                <xsl:value-of select="$rep_string"/>

您可以将其简化为以下内容:

<xsl:value-of select="replace(., ';', ',')"/>
相关问题