2013-08-28 21 views
0

我根据给定元素中的某些属性值拼凑了一个可生成过滤器属性值列表的例程。功能是这样的:尝试在for-each循环内获取父属性值

<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循环中做这样的事吗,还是我必须将它制动?

感谢您的任何帮助!

拉斯

回答

0

我认为你需要替换该行...

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

通过此行,而不是

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

当你做..[@arch] | ..[@condition] | ..[@security]所有你正在做的是选择父节点如果存在指定属性之一,那么当你真的试图获取属性本身时。

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

   <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(., ';', ',')"/> 
+0

我原以为我应该能够改变for-each语句。 (这是因为初始测试语句确定我们确实有一个具有所需属性的父元素,对吧?) 同样感谢您提供有关减少语句值的提示。 我会给这些尝试,但非常感谢你添! –