连接变量和查找表达式

huangapple go评论55阅读模式
英文:

Concatenate variable and lookup expression

问题

使用json-doc(),我将JSON存储在变量$db.data.work中。
然后我处理XML,其中有如下占位符:

<persName>{API:?*[?role_en='Composer']?person?firstName} {API:?*[?role_en='Composer']?person?lastName})</persName>

思路是使用"API:"后面的部分来从$db.data.work中检索值并替换占位符。如何将变量和查找表达式连接起来以便进行评估?

<template name="replace.api.placeholders">

    <variable name="pattern.api.placeholders" as="xs:string" expand-text="no">\{API:(.+?)\}</variable>
    <analyze-string select="." regex="{$pattern.api.placeholders}">
      <matching-substring>
        <value-of select="
            let $db.key := regex-group(1)
            return
              if ($db.data.work?{$db.key}) then
                $db.data.work?{$db.key}
              else
                ."/>
      </matching-substring>
      <non-matching-substring>
        <value-of select="."/>
      </non-matching-substring>
    </analyze-string>
  </template>

我尝试将两者连接为字符串,然后使用大括号或不使用大括号进行评估。

<value-of select="
                let $statement :=     '$db.data.work' || '?persons?*[?role_en=\"Composer\"]?person?firstName' 
                return
                  if ({$statement}) then
                            {$statement}
                  else              
                      ."/>

请注意,只注入一个键正常工作:

<value-of select="
                let $db.key := regex-group(1)
                return
                  if ($db.data.work?($db.key)) then
                    $db.data.work?($db.key)
                  else              
                      ."/>
英文:

Using json-doc() I store JSON in a variable $db.data.work.
Then I process XML where I have placeholders like this

<persName>{API:?*[?role_en='Composer']?person?firstName} {API:?*[?role_en='Composer']?person?lastName})</persName>

The idea is to use the part after "API:" in order to retrieve the values from $db.data.work and replace the placeholders. How could I concatenate the variable and the lookup expressions in order them to be evaluated?

<template name="replace.api.placeholders">

    <variable name="pattern.api.placeholders" as="xs:string" expand-text="no">\{API:(.+?)\}</variable>
    <analyze-string select="." regex="{$pattern.api.placeholders}">
      <matching-substring>
        <value-of select="
            let $db.key := regex-group(1)
            return
              if ($db.data.work<appended-retrieved-expression>) then
                $db.data.work<appended-retrieved-expression
                else
                  ."/>
      </matching-substring>
      <non-matching-substring>
        <value-of select="."/>
      </non-matching-substring>
    </analyze-string>
  </template>

I naively tried to concatenate both as strings and then evaluate using the curly braces or without.

    <value-of select="
                let $statement :=     '$db.data.work' || '?persons?*[?role_en=\"Composer\"]?person?firstName' 
                return
                  if ({$statement}) then
                            {$statement}
                  else              
                      ."/>

Note that just injecting one key works fine:

    <value-of select="
                let $db.key := regex-group(1)
                return
                  if ($db.data.work?($db.key)) then
                    $db.data.work?($db.key)
                  else              
                      ."/>

答案1

得分: 1

使用 xsl:evaluate

<xsl:evaluate xpath="regex-group(1)" context-item="$db.data.work"/>

需要 Saxon 10 或更高版本用于 HE,对于 PE 和 EE 9.8 或更高版本。

最小示例(您的输入):

<persName>{API:?*[?role_en=&#39;Composer&#39;]?person?firstName} {API:?*[?role_en=&#39;Composer&#39;]?person?lastName})</persName>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="#all"
	version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="persName">
    <xsl:variable name="pattern.api.placeholders" as="xs:string" expand-text="no">{API:(.+?)}</xsl:variable>
    <xsl:analyze-string select="." regex="{$pattern.api.placeholders}">
      <xsl:matching-substring>
        <xsl:evaluate xpath="regex-group(1)" context-item="$db.data.work"/>
      </xsl:matching-substring> 
    </xsl:analyze-string>
  </xsl:template>
  
  <xsl:param name="db.data.work" select="parse-json($json-string)"/>
  
  <xsl:param name="json-string" as="xs:string" expand-text="no">[
    { "role_en" : "Composer", "person": { "firstName": "Johann Sebastian", "lastName":"Bach" } }
  ]</xsl:param>
  
</xsl:stylesheet>
英文:

Use xsl:evaluate:

&lt;xsl:evaluate xpath=&quot;regex-group(1)&quot; context-item=&quot;$db.data.work&quot;/&gt;

Needs Saxon 10 or higher for HE, for PE and EE 9.8 or higher.

Minimal example (your input):

&lt;persName&gt;{API:?*[?role_en=&#39;Composer&#39;]?person?firstName} {API:?*[?role_en=&#39;Composer&#39;]?person?lastName})&lt;/persName&gt;

XSLT

&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
	xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
	exclude-result-prefixes=&quot;#all&quot;
	version=&quot;3.0&quot;&gt;

  &lt;xsl:mode on-no-match=&quot;shallow-copy&quot;/&gt;

  &lt;xsl:template match=&quot;persName&quot;&gt;
    &lt;xsl:variable name=&quot;pattern.api.placeholders&quot; as=&quot;xs:string&quot; expand-text=&quot;no&quot;&gt;\{API:(.+?)\}&lt;/xsl:variable&gt;
    &lt;xsl:analyze-string select=&quot;.&quot; regex=&quot;{$pattern.api.placeholders}&quot;&gt;
      &lt;xsl:matching-substring&gt;
        &lt;xsl:evaluate xpath=&quot;regex-group(1)&quot; context-item=&quot;$db.data.work&quot;/&gt;
      &lt;/xsl:matching-substring&gt; 
    &lt;/xsl:analyze-string&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:param name=&quot;db.data.work&quot; select=&quot;parse-json($json-string)&quot;/&gt;
  
  &lt;xsl:param name=&quot;json-string&quot; as=&quot;xs:string&quot; expand-text=&quot;no&quot;&gt;[
    { &quot;role_en&quot; : &quot;Composer&quot;, &quot;person&quot;: { &quot;firstName&quot;: &quot;Johann Sebastian&quot;, &quot;lastName&quot;:&quot;Bach&quot; } }
  ]&lt;/xsl:param&gt;
  
&lt;/xsl:stylesheet&gt;

huangapple
  • 本文由 发表于 2023年6月19日 23:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定