如何将 request:get-parameter 的值传递给 Xquery 中的变量,以便在 XSLT 模板中使用以生成 HTML 输出

问题描述 投票:0回答:1

我有这个 xml 文件:

<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>

我有一个网页,我想在其中搜索匹配的某些项目,假设 variant = 'Espessa' 并返回所有 @Inventory_number。 我使用 GET 传递插入到我的 html 表单中的值,然后我想创建一个 Xquery 代码来接收这些值,将这些值传递到 XSLT 代码中并返回一个包含找到的项目的 html 文件。

到目前为止,我的 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. 我决定首先尝试使用固定值,Variant = 'Espessa',但查询返回一个空页面。

  2. 我想在我的 XSLT 代码中使用变量 $fi 和 $fi2。

  3. 我的代码有什么问题?

  4. 如何在 xslt 代码中使用 $fi 和 $fi2 变量?

xslt get xquery exist-db
1个回答
0
投票

您需要实际评估来自 XQuery 的 XSLT,这可以通过调用

transform#1
函数来完成 - 请参阅:https://www.w3.org/TR/xpath-functions-31/#func-transform。从 XQuery 向 XSLT 传递参数有多种选择,但我将在此处仅包括一个这样的选项,我使用
xsl:param
.

你的

$filter
谓词也有一些问题。恐怕XML不是由
key=value
对组成的,而是一棵树and,所以谓词需要查询树中正确的节点。

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
      }
  })

我已经对您要实现的目标做出了一些假设,但希望这对您来说是一个很好的例子。随时提出任何其他问题...

© www.soinside.com 2019 - 2024. All rights reserved.