XSLT确定参数值是否包含基于xslt输入循环的值

时间:2013-11-12 05:25:39

标签: xml xslt xslt-1.0 composite

我正在尝试循环遍历XML数据,以确定XML输入的子字符串是否包含在参数TagID中。以下是输入的示例:

 <in:inputs xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
<!-- Input Parameter, XPath /in:inputs/in:param[@name='TagTypeCode'] -->
<in:param name="TagTypeCode">USC_CATEGORY_tags</in:param>
<!-- Input Parameter, XPath /in:inputs/in:param[@name='keyword'] -->
<in:param name="keyword"/>
<!-- Input Parameter, XPath /in:inputs/in:param[@name='TagIDs'] -->
<in:param name="TagIDs">,2704,2764,</in:param>
<!-- Function Call Result (2 ms), XPath /in:inputs/in:result[@name='SolrSearchResults']/response -->
 <in:result name="SolrSearchResults">
     <response xmlns="">
         <lst name="responseHeader">
            <int name="status">0</int>
            <int name="QTime">1</int>
             <lst name="params">
                <str name="facet">true</str>
                <str name="start">0</str>
                <str name="q">*:* AND tagids:2704 AND tagids:2764</str>
                <str name="facet.field">USC_CATEGORY_tags</str>
                <str name="rows">0</str>
            </lst>
        </lst>
        <result name="response" numFound="0" start="0"/>
         <lst name="facet_counts">
            <lst name="facet_queries"/>
             <lst name="facet_fields">
                 <lst name="USC_CATEGORY_tags">
                    <int name="2703.Best Sellers">0</int>
                    <int name="2704.New Release">0</int>
                    <int name="2705.Training Products">0</int>
                    <int name="2706.AICP Exam Prep">0</int>
                    <int name="2707.PAS Reports">0</int>
                    <int name="2709.PAS Super Sale">0</int>
                    <int name="2722.Books Big Sale 2013">0</int>
                    <int name="2723.Books Big Sale 2013 - 20% Off">0</int>
                    <int name="2724.Books Big Sale 2013 - 50% Off">0</int>
                    <int name="2725.Books Big Sale 2013 - 80% Off">0</int>
                    <int name="2734.eBook Landing Page">0</int>
                    <int name="2736.eBook Landing Page">0</int>
                </lst>
            </lst>
            <lst name="facet_dates"/>
            <lst name="facet_ranges"/>
        </lst>
    </response>
</in:result>

是否可以遍历/ in:inputs / in:result [@ name ='SolrSearchResults'] / response / lst [@ name ='facet_counts'] / lst [@ name ='facet_fields'] / lst / INT 确定TagIDs参数中是否包含任何 @name属性('。'之前的子字符串),如果是,将变量设置为某个值?什么是最有效的方式?

这是我尝试过的事情

<xsl:variable name="root" select="/in:inputs/in:result[@name='SolrSearchResults']/response" />
    <xsl:variable name="TagIDs" select="/in:inputs/in:param[@name='TagIDs']" />
    <xsl:variable name="facet_selected" select="0"/>

<xsl:for-each select="$root/lst[@name='facet_counts']/lst[@name='facet_fields']/lst/int">
            <xsl:if test="contains($TagIDs,concat(',',substring-before(@name,'.'),','))" >
                <xsl:variable name="facet_selected" select="1" />
            </xsl:if>
        </xsl:for-each>

如果你能指出我正确的方向,我会非常感激!

1 个答案:

答案 0 :(得分:1)

是的,你可以这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:variable name="root"
                  select="/in:inputs/in:result[@name='SolrSearchResults']
                                    /response" />
    <xsl:variable name="TagIDs"
                  select="/in:inputs/in:param[@name='TagIDs']" />
    <xsl:variable name="ints"
                  select="$root/lst[@name='facet_counts']
                               /lst[@name='facet_fields']/lst/int" />

    <xsl:variable name="matchingInts"
                  select="$ints[contains($TagIDs, 
                                         concat(',', 
                                                substring-before(@name, '.'), 
                                                ',')
                                        )]" />
    <!-- facet_selected will have the value 1 if matches were found, 0 if not -->
    <xsl:variable name="facet_selected" select="number(boolean($matchingInts))"/>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时,matchingInts包含名称为int的{​​{1}},2704.New Release的值为1.