Question

I want to select nodes by the existence of values of 2 childnodes, e.g. give me the node that has value 5 for objecttype node and value 2 for objectid node.

The objectid and objecttype combination is unique, so it will always return a single result.

What would be the XSL statement?

<response>
    <result name="response" numFound="5" start="0">
        <doc>
            <str name="title">Tours</str>
            <int name="objecttype">5</int>
            <str name="friendlyurl">tours</str>
            <str name="avatar">2_156_DSC01511.JPG</str>
            <int name="objectid">2</int>
        </doc>
        <doc>
            <str name="title">Celebrations Car</str>
            <int name="objecttype">5</int>
            <str name="friendlyurl">celebrations-car</str>
            <str name="avatar">3_583_0509-0257-20x30-framed.jpg</str>
            <int name="objectid">3</int>
        </doc>
    </result>
</response>
Was it helpful?

Solution 2

I assume you want to select the doc node based on its children? If so, given your above xml sample , you would have to create an xpath which selects doc based on two predicates (which each have a predicate of their own. It might look something like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">

        <xsl:copy-of select="response/result/doc[int[@name='objecttype'] = '5' and int[@name='objectid'] = '2']" />

    </xsl:template>

</xsl:stylesheet>

I've tested this locally and the output is:

<doc>
    <str name="title">Tours</str>
    <int name="objecttype">5</int>
    <str name="friendlyurl">tours</str>
    <str name="avatar">2_156_DSC01511.JPG</str>
    <int name="objectid">2</int>
</doc>

OTHER TIPS

Consider you have your search criteria in $id and $type.

If you have a small document:

/response/result/doc[int[@name='objectid']=$id and int[@name='objecttype']=$type]

If you have thousands of <doc> elements:

<xsl:key name="lookup" match="doc"
         use="concat(int[@name='objectid'],'&#xd;',int[@name='objecttype'])"/>

...

  select="key('lookup',concat($id,'&#xd;',$type))"

These work in both XSLT 1.0 and XSLT 2.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top