开发者

Filter XML from searchparameters

I'm creating a small search function on a site I'm creating. I'm working with Umbraco CMS, and all the nodes that I need to search are children of th开发者_运维百科e page that are being searched on. Right now I have a search box with seven fields. three input fields and four dropdown's.

This is the XML that I need to find my results from:

<Modellist id="1073" parentID="1052" level="2" writerID="0" creatorID="0" nodeType="1065" template="1066" sortOrder="0" createDate="2010-07-12T20:23:35" updateDate="2010-07-12T21:25:13" nodeName="Piger-girls" urlName="piger-girls" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073" isDoc="">
<title>Girls</title>
<metaDescription></metaDescription>
<metaTitle />
<metaKeywords />
<nameInMenu />
<Model id="1075" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="1" createDate="2010-07-12T20:25:31" updateDate="2010-07-12T21:01:58" nodeName="Lene Madsen" urlName="lene-madsen" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1075" isDoc="">
    <title>Lene M</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>014</modelnummer>
    <height>120</height>
    <size>122</size>
    <eyeColor>Blå</eyeColor>
    <hairColor>Blond</hairColor>
    <shoeSize>26-30</shoeSize>
</Model>
<Model id="1077" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="2" createDate="2010-07-13T19:04:42" updateDate="2010-07-13T19:05:23" nodeName="Julie B" urlName="julie-b" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1077" isDoc="">
    <title>Julie B</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>002</modelnummer>
    <height>129</height>
    <size>104</size>
    <eyeColor>Grøn</eyeColor>
    <hairColor>Sort</hairColor>
    <shoeSize>26-30</shoeSize>
</Model>
<Model id="1078" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="3" createDate="2010-07-13T19:05:38" updateDate="2010-07-13T19:06:06" nodeName="Marlene U" urlName="marlene-u" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1078" isDoc="">
    <title>Marlene U</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>007</modelnummer>
    <height>89</height>
    <size>86</size>
    <eyeColor>Grå</eyeColor>
    <hairColor>Blond</hairColor>
    <shoeSize>26-30</shoeSize>
</Model>
<Model id="1079" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="4" createDate="2010-07-13T19:06:18" updateDate="2010-07-13T19:07:20" nodeName="Louise N" urlName="louise-n" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1079" isDoc="">
    <title>Louise N</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>022</modelnummer>
    <height>148</height>
    <size>152</size>
    <eyeColor>Brun</eyeColor>
    <hairColor>Brun</hairColor>
    <shoeSize>31-35</shoeSize>
</Model>
<Model id="1080" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="5" createDate="2010-07-13T19:07:35" updateDate="2010-07-13T19:07:59" nodeName="Marie A" urlName="marie-a" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1080" isDoc="">
    <title>Marie A</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>010</modelnummer>
    <height>146</height>
    <size>128</size>
    <eyeColor>Blå</eyeColor>
    <hairColor>Rød</hairColor>
    <shoeSize>36-40</shoeSize>
</Model>
<Model id="1081" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="6" createDate="2010-07-13T19:08:22" updateDate="2010-07-13T19:08:53" nodeName="Sandra F" urlName="sandra-f" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1081" isDoc="">
    <title>Sandra F</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>013</modelnummer>
    <height>126</height>
    <size>122</size>
    <eyeColor>Blå</eyeColor>
    <hairColor>Brun</hairColor>
    <shoeSize>31-35</shoeSize>
</Model>
<Model id="1082" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="7" createDate="2010-07-13T19:09:10" updateDate="2010-07-13T19:09:51" nodeName="Laura N" urlName="laura-n" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1082" isDoc="">
    <title>Laura N</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>004</modelnummer>
    <height>73</height>
    <size>74</size>
    <eyeColor>Grøn</eyeColor>
    <hairColor>Rød</hairColor>
    <shoeSize>18-20</shoeSize>
</Model>
<Model id="1083" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="8" createDate="2010-07-13T19:10:09" updateDate="2010-07-13T19:11:19" nodeName="Gitte R" urlName="gitte-r" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1083" isDoc="">
    <title>Gitte R</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>017</modelnummer>
    <height>105</height>
    <size>110</size>
    <eyeColor>Grøn</eyeColor>
    <hairColor>Brun</hairColor>
    <shoeSize>21-25</shoeSize>
</Model>
<Model id="1084" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="9" createDate="2010-07-13T19:11:30" updateDate="2010-07-13T19:12:56" nodeName="Mia H" urlName="mia-h" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1084" isDoc="">
    <title>Mia H</title>
    <images></images>
    <polaroid />
    <videoSmall />
    <videoLarge />
    <modelnummer>015</modelnummer>
    <height>138</height>
    <size>140</size>
    <eyeColor>Blå</eyeColor>
    <hairColor>Rød</hairColor>
    <shoeSize>31-35</shoeSize>
</Model>

The tags called "size","eyeColor","hairColor" and "shoeSize" shall be matched with the four dropdowns in my search box. The tag called "title" can be searched from a input field called name. And then I have another to textfields called "heightFrom" and "heightTo". here a user can search on height, and this should be matched against the XML-tag called "height".

It's easy for me to say that if the name-field is not empty I can just do this:

<xsl:apply-templates select="$currentPage/*[@isDoc][contains(Exslt.ExsltStrings:lowercase(./title),$name)]">
                <xsl:sort data-type="text" select="title" order="ascending"/>
            </xsl:apply-templates>

$name contains the searched name. And this will give me all the nodes that have the searched name in the "title"-tag. But how do I get on from this. Of course I can create a choose, with a lot (more than 40) of when's in it, but that would be a big job to that, and very difficult to maintain in the future. One of the difficult things are that none of the fields are mandatory, so I don't know which of the fields will be filled out. Can I do anything else?

By the way - a URL could in example look like this: http://my.site/search.aspx?hf=150&ht=&size=62&ec=&hc=&ss=&name=Julie

I have all of the values in seven variables like this:

<xsl:variable name="heightFrom" select="umbraco.library:Request('hf')"/>
<xsl:variable name="heightTo" select="umbraco.library:Request('ht')"/>
<xsl:variable name="size" select="umbraco.library:Request('size')"/>
<xsl:variable name="eyeColor" select="umbraco.library:Request('ec')"/>
<xsl:variable name="shoeSize" select="umbraco.library:Request('ss')"/>
<xsl:variable name="hairColor" select="umbraco.library:Request('hc')"/>
<xsl:variable name="name" select="Exslt.ExsltStrings:lowercase(umbraco.library:Request('name'))"/>

Any help and inputs will be greatly appreaciated!

Thanks in advance :)

/Kim


I think you only need an XPath expression in the correct context. This styleshet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="heightFrom" select="120"/>
    <xsl:param name="heightTo" select="130"/>
    <xsl:param name="size" />
    <xsl:param name="eyeColor" />
    <xsl:param name="shoeSize" />
    <xsl:param name="hairColor" />
    <xsl:param name="name" />
    <xsl:template match="/">
        <Modellist>
            <xsl:copy-of select="*/Model[height >= $heightFrom and $heightTo >= height
                              or
                             height >= $heightFrom and not($heightTo)
                              or
                             not($heightFrom) and $heightTo >= height]
                            [size = $size or not($size)]
                            [eyeColor = $eyeColor or not($eyeColor)]
                            [$shoeSize >= substring-before(shoeSize,'-')
                              and 
                             substring-after(shoeSize,'-') >= $shoeSize
                              or
                             not($shoeSize)]
                            [hairColor = $hairColor or not($hairColor)]
                            [title = $name or not($name)]"/>
        </Modellist>
    </xsl:template>
</xsl:stylesheet>

Result:

<Modellist>
    <Model id="1075" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="1" createDate="2010-07-12T20:25:31" updateDate="2010-07-12T21:01:58" nodeName="Lene Madsen" urlName="lene-madsen" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1075" isDoc="">
        <title>Lene M</title>
        <images></images>
        <polaroid />
        <videoSmall />
        <videoLarge />
        <modelnummer>014</modelnummer>
        <height>120</height>
        <size>122</size>
        <eyeColor>Blå</eyeColor>
        <hairColor>Blond</hairColor>
        <shoeSize>26-30</shoeSize>
    </Model>
    <Model id="1077" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="2" createDate="2010-07-13T19:04:42" updateDate="2010-07-13T19:05:23" nodeName="Julie B" urlName="julie-b" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1077" isDoc="">
        <title>Julie B</title>
        <images></images>
        <polaroid />
        <videoSmall />
        <videoLarge />
        <modelnummer>002</modelnummer>
        <height>129</height>
        <size>104</size>
        <eyeColor>Grøn</eyeColor>
        <hairColor>Sort</hairColor>
        <shoeSize>26-30</shoeSize>
    </Model>
    <Model id="1081" parentID="1073" level="3" writerID="0" creatorID="0" nodeType="1067" template="1068" sortOrder="6" createDate="2010-07-13T19:08:22" updateDate="2010-07-13T19:08:53" nodeName="Sandra F" urlName="sandra-f" writerName="Administrator" creatorName="Administrator" path="-1,1052,1073,1081" isDoc="">
        <title>Sandra F</title>
        <images></images>
        <polaroid />
        <videoSmall />
        <videoLarge />
        <modelnummer>013</modelnummer>
        <height>126</height>
        <size>122</size>
        <eyeColor>Blå</eyeColor>
        <hairColor>Brun</hairColor>
        <shoeSize>31-35</shoeSize>
    </Model>
</Modellist>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜