How can I search an XML file without a dynamic language?
Let me try to explain my situation:
We are using a CMS which 'bakes' a website, and you publish it to a webserver. The published site contains only static HTML ( or XML ) pages ( generated from the content in the CMS database ).
I imported an XML file with the names and phone numbers from the company phone directory.
Using only XSLT, can I create a way to search that directory?
For example, if my XML file, directory.xml looks like this:
<directory>
<person>
<fname>Ryan</fname>
<lname>Purple</lname>
<phone>887 778 5544</phone>
</person>
<person>
<fname>Tanya</fname>
<lname>Orange</lname>
<phone>887 998 5541</phone>
</person>
<directory>
Can I create a way to search for a person with the last name starting with "Pur" ?
Can I 开发者_如何学运维pass a parameter to the XSLT?
Can I search the XML tree to match the string in the parameter?
Using only XSLT, can I create a way to search that directory?
Yes.
Can I create a way to search for a person with the last name starting with "Pur" ?
Yes. In fact, the transformation below allows to search for text starting with any 2,3,4 or 5 characters. It can be generalized to allow search for a starting string up to any predefined maximum length.
1.Can I pass a parameter to the XSLT?
Yes. The details how to do this depend on the particular XSLT processor that is used. For example here is how to pass external parameters to the .NET XslCompiledTransform.Transform()
2.Can I search the XML tree to match the string in the parameter?
Yes. This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pPattern" select="'Pur'"/>
<xsl:key name="kPersonByLNameStart"
match="person" use="substring(lname,1,2)"/>
<xsl:key name="kPersonByLNameStart"
match="person" use="substring(lname,1,3)"/>
<xsl:key name="kPersonByLNameStart"
match="person" use="substring(lname,1,4)"/>
<xsl:key name="kPersonByLNameStart"
match="person" use="substring(lname,1,5)"/>
<xsl:template match="/">
<results>
<xsl:copy-of select=
"key('kPersonByLNameStart', $pPattern)"/>
</results>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document (the provided XML document -- corrected to be well-formed and extended):
<directory>
<person>
<fname>Ryan</fname>
<lname>Purple</lname>
<phone>887 778 5544</phone>
</person>
<person>
<fname>Tanya</fname>
<lname>Orange</lname>
<phone>887 998 5541</phone>
</person>
<person>
<fname>Martin</fname>
<lname>Purr</lname>
<phone>887 778 5544</phone>
</person>
</directory>
produces the wanted, correct results and in the most efficient way:
<results>
<person>
<fname>Ryan</fname>
<lname>Purple</lname>
<phone>887 778 5544</phone>
</person>
<person>
<fname>Martin</fname>
<lname>Purr</lname>
<phone>887 778 5544</phone>
</person>
</results>
Do Note:
This code shows how to search efficiently for text having some prefix of length 2 or 3 or 4 or 5.
How about AJAX? That should run without server-side assistance and will read your xml perfectly. W3Schools has a good intro.
Edited: Blah, sorry, that's useless..I'd forgotten that even here, you need to use a server-side script :/
精彩评论