【问题标题】:How to insert a variable into an element's and/or attribute´s value in xslt如何在 xslt 中将变量插入到元素和/或属性的值中
【发布时间】:2019-05-07 14:16:26
【问题描述】:

我在我的 BPEL 中使用 xslt 将我的有效负载转换为我正在使用的具有 SQL 命令的 Web 服务的可接受格式。现在我的有效负载中有这个元素,我需要在我的搜索查询中使用它。

例如:

<DocumentID>12341241123-124124-124124</DocumentID>

我想在我的 SQL 查询中使用这个元素“DocumentID”的值作为参数。在以下意义上:

        <?xml version="1.0" encoding="UTF-8" ?>
        <xsl:stylesheet version="1.0"

    <!-- a whole bunch of namespaces -->

           <xsl:template match="/">
              <xsl:for-each select="current()[string-length(DocumentID) >= $STRING_LENGTH]/DocumentID">
                 <xsl:element name="SearchRequest" type="SomeRequestType">
                 <xsl:element name="SearchScope" type="SomeStoreScope" 
                                   objectStore="SomeObjectStore"/>
                 <xsl:element name="SearchSQL">SELECT [Id] FROM Document WHERE
                                               VersionNumber ='{12341241123-124124-124124}'
                                               AND SecondStatement = true</xsl:element>
                 </xsl:element>
              </xsl:for-each>
           </xsl:template>
        </xsl:stylesheet>

但是,在前面的示例中,“VersionNumber”是硬编码的。我希望这是一个用我的有效负载中的实际“DocumentID”元素覆盖的参数。

xslt 中是否有任何方法可以从现有的有效负载元素中获取值并将其用作另一个元素的值的一部分。如果应该将变量插入到所述元素的属性中,该怎么做?

按这个顺序有什么东西吗?

         <xsl:element name="SearchSQL">SELECT [Id] FROM Document WHERE
                                       VersionNumber ='$DocumentID'
                                       AND SecondStatement = true</xsl:element>
         </xsl:element>

提前感谢您的帮助,请随时要求澄清!

干杯,

杰斯帕

【问题讨论】:

标签: xml xslt soa bpel


【解决方案1】:

在 XSLT 3.0 中,设置 expand-text="yes" 并写入 VersionNumber='{$DocumentID}'

在早期版本中写VersionNumber='&lt;xsl:value-of select="$DocumentID"/&gt;'

另请注意,xsl:element 在 version="2.0" 之前没有 type 属性,并且除非有 xsl:import-schema 声明,否则设置 type 属性没有意义。

【讨论】:

  • 非常感谢迈克尔。我确实有一个问题:如果目标是一个属性而不是所描述的值的一部分怎么办?例如:我需要获取“DocumentID”的值并将其放入属性 targetID 中。 ?
  • 自 XSLT 1.0 以来,您一直能够使用属性值模板:targetID="{$variable}"。我总是对有些人似乎花了数年时间编写 XSLT 却没有发现这个特性感到惊讶,对我来说,这是你在任何课程的第一天就教的东西。
【解决方案2】:

我最终以与 Michael Kay 的答案略有不同的方式解决了这个问题,我想将其包含在内以完成:

<xsl:template match="/">
   <SearchRequest type="SomeRequestType" xmlns="http://www.w3.org/2001/XMLSchema-instance">
      <SearchScope type="SomeStoreScope" objectStore="SomeObjectStore"/>
      <SearchSQL>
         <xsl:text>SELECT [Id] FROM Document WHERE VersionNumber = </xsl:text>
         <xsl:value-of select="/Stuff/MoreStuff/DocumentID"/>
         <xsl:text> AND SecondStatement = true </xsl:text>
      </SearchSQL>
   </SearchRequest>
</xsl:template>

它就像这样完美地工作! 在另一种情况下,我想将变量 DocumentID 插入属性中,并以以下方式进行:

<Target>
   <xsl:attribute name="objectId">
     <xsl:value-of select="/Stuff/MoreStuff/DocumentID"/>
   </xsl:attribute>
   <xsl:attribute name="objectStore">
     <xsl:text disable-output-escaping="no">SomeObjectStore</xsl:text>
   </xsl:attribute>
</Target>

我希望这对某人有所帮助! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多