xslt中的动态xpath? [英] dynamic xpath in xslt?

查看:24
本文介绍了xslt中的动态xpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件集:

源文件.xml:

      <?xml version="1.0" encoding="utf-8" ?>
     <Employees>
     <Employee id="1">
          <firstname relationship="headnote">Atif</firstname>
          <lastname relationship="lname">Bashir</lastname>
          <age relationship="age">32</age>
          </Employee>
     </Employees>

参数设置.xml

        <?xml version="1.0" encoding="utf-8"?>
        <Settings>
        <Employee id="1">
             <sourceFile>Lookup1.xml</sourceFile>
             <sourceXpathfield>Employees/Employee[@id</sourceXpathfield>
             <lookupXpathfield>Employees/Employee[@id='1']</lookupXpathfield>
             <elementstoinsert>xyz</elementstoinsert>
             </Employee>
         </Settings>

Lookup.xml

<?xml version="1.0" encoding="utf-8"?>
 <Employees>
  <Employee id="1">
      <department code="102">HR</department>
   </Employee>
   </Employees>

transform.xsl

transform.xsl

  <?xml version="1.0" encoding="UTF-8" ?>
   <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:include href="identity.xsl"/>

  <xsl:param name="EmployeeId" select="'1,2'" />
  <xsl:variable name="FileSettings" select="document('test3.xml')" />
  <xsl:variable name="SuppressSetting" select="$FileSettings/Settings/Employee[@id = tokenize($EmployeeId, ',')]" />

  <xsl:template match="Employee">
  <xsl:copy>
  <xsl:apply-templates select="@*"/>
  <xsl:apply-templates select="publisher" />
  <xsl:apply-templates select="node() except publisher"/>
  <xsl:variable name="outerfile" select="document($SuppressSetting/sourceFile)"></xsl:variable>
  <xsl:variable name="outerfiledetails" select="$outerfile/$SuppressSetting/lookupXpathfield"></xsl:variable>
  <xsl:value-of select="$outerfiledetails"></xsl:value-of>
</xsl:copy>
</xsl:template>

</xsl:stylesheet> 

输出应该是:

     <?xml version="1.0" encoding="utf-8" ?>
     <Employees>
     <Employee id="1">
          <firstname relationship="headnote">Atif</firstname>
          <lastname relationship="lname">Bashir</lastname>
          <age relationship="age">32</age>
          HR
          </Employee>
     </Employees>

我在 Transform.xsl 中更改了以下行

I changed the below line in Transform.xsl

<xsl:variable name="outerfiledetails" select="$outerfile/$SuppressSetting/lookupXpathfield"></xsl:variable>

进入

<xsl:variable name="outerfiledetails" select="$outerfile/Employees/Employee[@id='1']"></xsl:variable>

然后我得到了我的输出,但我想将 SourceFile.xmlLookup.xml 的 XPath epression 保留到 ParamerterSettings.xml 中> 以便我可以编写更通用的脚本.这可以通过动态xpath以外的任何其他方式完成吗?任何想法或提示都将受到高度赞赏.

then I am getting my output but I want to keep the XPath epression for both SourceFile.xml and Lookup.xml into ParamerterSettings.xml so that I can write a more generic script. Can this be done in any other way then the dynamic xpath? Any idea or hint to impelement the same will be highly appreciated.

推荐答案

动态 XPath 评估在纯 XSLT 1.0 或 2.0 中是不可能的.

在混合"模式中至少有三种方法可以做到这一点.解决方案:

我.使用 EXSLT 函数 dyn:evaluate()

遗憾的是,很少有 XSLT 1.0 处理器实现 dyn:evaluate().

Unfortunately, very few XSLT 1.0 processors implement dyn:evaluate().

二.使用 XSLT 处理 XML 文档并生成包含 XPath 表达式的新 XSLT 文件——然后执行新生成的转换.

很少有人这样做,在我看来,这比下一个解决方案更复杂.

Very few people do this and, in my opinion, this is more complex than the next solution.

三.XPath Visualizer 的工作方式

III. The way the XPath Visualizer works

想法是:

  1. 在 XSLT 样式表中有一个像这样定义的全局变量:

  <xsl:variable name="vExpression" select="dummy"/>

  • 然后,使用 DOM 将样式表加载为 XML 文档,并将 vExpression 变量的 select 属性 替换为包含在源 XML 文档中的实际 XPath 表达式.

  • Then, load the stylesheet as an XML document using DOM, and replace the select attribute of the vExpression variable with the actual XPath expression that is contained in the source XML document.

    最后,使用加载到内存中并动态更新的 xslt 样式表启动转换.


    四.使用 XSLT 3.0

    使用 <xsl:evaluate>说明

    Use the <xsl:evaluate> instruction

    这篇关于xslt中的动态xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆