1

我已经查看了有关嵌套选择的其他一些帖子,但不相信它们解决了我的用例。本质上,我正在尝试通过 Web 服务在另一个系统中创建一个用户帐户,并且需要传递一个登录 ID,该登录 ID 源自我的 xml 中的一个字段,该字段基本上可以是任何东西,例如员工 ID、电子邮件地址、UUID 等。要使用的字段将来自生成 xml 的配置值。为简单起见,我将我的 xml 和 xslt 缩写,所以请不要建议我使用选择或 if 语句,因为我需要保持可能的 xml 字段从广泛开放中进行选择。

示例 XML:

<root>
  <General>
    <Name Prefix="MR" First="Mickey" Middle="M" Last="Mouse" Suffix="I" Title="BA" Gender="M" BirthMonth="02" BirthDay="26" BirthYear="1984"/>
    <Email Work="test9999@acme.com" Home="Homeemail@gmail.com"/>
    <EmployeeId>9948228</EmployeeId>
  </General>
  <ConfigProperties>
    <LoginID>root/General/EmployeeId</LoginID>
  </ConfigProperties>
</root>

XSL 示例:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8" indent="yes" />
<xsl:template match="/">
  <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:variable name="xxLI" select="root/ConfigProperties/LoginID" />
    <xsl:attribute name="LoginId"><xsl:value-of select="$xxLI"/></xsl:attribute>
  </Response>
</xsl:template>
</xsl:stylesheet>

转换后的 XML:

<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          LoginId="root/General/EmployeeId"/>

我真正希望回来的是:

<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          LoginId="9948228"/>

我难住了。有什么想法吗?

4

2 回答 2

1

在普通的 XSLT 1 中没有办法做到这一点,但是如果您的 XSLT 处理器支持“动态”扩展(XALAN 支持它),您可以这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dyn="http://exslt.org/dynamic"
    extension-element-prefixes="dyn">

    <xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8" indent="yes" />
    <xsl:template match="/">
        <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <xsl:variable name="xxLI" select="root/ConfigProperties/LoginID" />
            <xsl:attribute name="LoginId"><xsl:value-of select="dyn:evaluate($xxLI)"/></xsl:attribute>
        </Response>
    </xsl:template>
</xsl:stylesheet>

我使用 XALAN 在 Oxygen/XML 中对此进行了测试并得到了这个输出

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" LoginId="9948228"/>
于 2018-07-05T15:56:23.990 回答
0

谢谢——花了几个小时在 libxslt 中正确实施后,工作就像一个魅力。对于任何使用 c 感兴趣的人,请声明以下内容:

#include <libexslt/exslt.h>
#include <libexslt/exsltconfig.h>

然后在您的代码中包含以下行:

exsltRegisterAll();

并确保在编译时引用该库

-lexslt
于 2018-07-05T19:26:02.690 回答