Marco Oliveira
Marco Oliveira

Reputation: 1

How to pass the values of request:get-parameter to the variables in Xquery to be used in a XSLT template to generate a HTML output

I have this xml file:

<A9-ArchaeologicalExcavation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <E53-Place>
        <Name>asdad</Name>
        <District/>
        <Parish/>
        <Gps>
            <Latitude/>
            <Longitude/>
        </Gps>
        <A2-StratigraphicVolumeUnit>
            <Timeline/>
            <Description/>
            <Gps>
                <Latitude/>
                <Longitude/>
            </Gps>
            <S19-EncounterEvent>
                <Date>2001-11-12</Date>
                <E24-PhysicalManThing Inventory_number="1">
                    <E36-VisualItem/>
                    <Order_number>Triangular</Order_number>
                    <Group>
                        <Group-name>Reta</Group-name>
                        <Sub_type>true</Sub_type>
                    </Group>
                    <E3-ConditionState>
                        <E55-Type/>
                    </E3-ConditionState>
                    <E55-Type>Alongado</E55-Type>
                    <Variant>Espessa</Variant>
                    <Typometry Unit="mm">
                        <Lenght>23</Lenght>
                        <Width>12</Width>
                        <Thickness>5</Thickness>
                        <Body_lenght>19</Body_lenght>
                        <Base_lenght>4</Base_lenght>
                    </Typometry>
                    <Morphology>
                        <Point>true</Point>
                        <Body>false</Body>
                        <Base>Triangular</Base>
                    </Morphology>
                    <Retouch>
                        <Location/>
                        <Mode/>
                        <Amplitude/>
                        <Direction/>
                        <Delineation/>
                        <Orientation/>
                        <Shape/>
                    </Retouch>
                    <Raw_material/>
                    <Observations/>
                </E24-PhysicalManThing>
            </S19-EncounterEvent>
        </A2-StratigraphicVolumeUnit>
    </E53-Place>
</A9-ArchaeologicalExcavation>

I have a web page where i want to search for a certain items that match, lets say variant = 'Espessa' and return all the @Inventory_number. I use GET to pass the values inserted in my html form and then i want to create an Xquery code that recieves the values, passes those values into the XSLT code and returns an html file with the items found.

So far i have this in my Xquery:

xquery version "3.0" encoding "UTF-8";
declare variable $fi as xs:integer :=5;
declare variable $fi2 as xs:string := "variant";
let $fi:= request:get-parameter("termo", 5)
let $fi2 :=request:get-parameter("elementos", "variant")
let $x := doc("/db/apps/MegaLOD/arrowheads/arrowheads.xml")
let $filter := concat($fi2, " = ", $fi, "")
for $arrowheads in $x/arrowheads
let $arrowhead_list :=
    (for $arrowhead in $arrowheads/arrowhead[$filter]
    return $arrowhead)

return
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html> 
    <body>
      <h2>Arrowheads</h2>
      
        <xsl:for-each select="A9-ArchaeologicalExcavation/E53-Place/A2-StratigraphicVolumeUnit/S19-EncounterEvent/E24-PhysicalManThing[Variant = 'Espessa']">
    
        <p><xsl:value-of select="@Inventory_number"/></p>
    
        </xsl:for-each>
    
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
  1. I decided first to try with fixed values, Variant = 'Espessa', but the query returns an empty page.

  2. I want to use the variables $fi and $fi2 in my XSLT code.

  3. What is wrong in my code?

  4. How can I use $fi and $fi2 variables in the xslt code?

Upvotes: 0

Views: 46

Answers (1)

adamretter
adamretter

Reputation: 3517

you need to actually evaluate the XSLT from XQuery, that can be done by calling the transform#1 function - see: https://www.w3.org/TR/xpath-functions-31/#func-transform. There are several options for passing in parameters to XSLT from XQuery, but I will include just one such option here where I use xsl:param.

You also have some problems in your $filter predicate. I am afraid that XML is not composed of key=value pairs, instead it is a tree and, so the predicate needs to query the correct node in the tree.

xquery version "3.1" encoding "UTF-8";

declare variable $default-elementos := "Variant";
declare variable $default-termo := "Espessa";

let $elementos :=request:get-parameter("elementos", $default-elementos)
let $termo:= request:get-parameter("termo", $default-termo)

let $matches := doc("/db/apps/MegaLOD/arrowheads/arrowheads.xml")//element()[local-name(.) = $elementos][. eq = $termo]

let $things := $matches/parent::element()

let $xslt :=
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    
      <xsl:param name="elementos" required="yes"/>
      <xsl:param name="termo" required="yes"/>
    
      <xsl:template match="results">
        <html>
          <body>
            <div>
              <p><b>Elementos:</b> <xsl:copy-of select="$elementos"/></p>
              <p><b>Termo:</b> <xsl:copy-of select="$termo"/></p>
            </div>
            <h2>Arrowheads</h2>
            <xsl:apply-templates select="node() | @*"/>
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="E24-PhysicalManThing">
        <p>
          <xsl:value-of select="@Inventory_number"/>
        </p>
      </xsl:template>
    
      <!-- Fall through to identity transform -->
      <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>

return
  transform(map {
      "stylesheet-node": $xslt,
      "source-node": <results>{$things}</results>,
      "stylesheet-params": map {
          "elementos": $elementos,
          "termo": $termo
      }
  })

I have made a few assumptions about what you are trying to achieve, but hopefully this will serve as a good example for you. Feel free to ask any further questions...

Upvotes: 0

Related Questions