xslt嵌套从xml节点值中选择
我已经回顾了其他一些关于嵌套选择的帖子,但我认为它们并没有涉及我的用例。本质上,我试图通过web服务在另一个系统中创建一个用户帐户,并且需要传递一个登录ID,该ID来自xml中的字段,该字段基本上可以是任何内容,例如员工ID、电子邮件地址、UUID等。要使用的字段将来自生成xml的配置值。为了简单起见,我对xml和xslt进行了缩写,因此请不要建议我使用choose或if语句,因为我需要保持可能的xml字段完全开放 示例XML:xslt嵌套从xml节点值中选择,xml,xslt,libxslt,Xml,Xslt,Libxslt,我已经回顾了其他一些关于嵌套选择的帖子,但我认为它们并没有涉及我的用例。本质上,我试图通过web服务在另一个系统中创建一个用户帐户,并且需要传递一个登录ID,该ID来自xml中的字段,该字段基本上可以是任何内容,例如员工ID、电子邮件地址、UUID等。要使用的字段将来自生成xml的配置值。为了简单起见,我对xml和xslt进行了缩写,因此请不要建议我使用choose或if语句,因为我需要保持可能的xml字段完全开放 示例XML: <root> <General>
<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>
9948228
root/General/EmployeeId
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"/>
我被难住了。有什么想法吗?在普通XSLT1中没有办法做到这一点,但是如果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"/>
谢谢-花了几个小时在libxslt中进行正确的实现后,工作非常出色。对于使用c的任何感兴趣的人,请声明以下内容:
#include <libexslt/exslt.h>
#include <libexslt/exsltconfig.h>
并确保编译时引用库
-lexslt
您是否仅限于XSLT1?不幸的是,是的,因为我必须走老路,将libxml/libxslt与c结合使用。
-lexslt