XSLT中的动态变量访问

XSLT中的动态变量访问,xslt,variables,Xslt,Variables,我想定义一个xslt表,其中定义了变量和访问函数。然后应该在另一个样式表中使用该样式表。其主要思想是为接口映射任务定义键值对。以下代码不会产生结果 编辑:现实世界的问题是一个系统集成问题。考虑一个“类型”属性。在系统A中,“Car”类型用键“1”进行编码。在系统B中(我必须从系统a导入消息),类型“Car”用键“X”编码。示例:1辆车X。因此,当我将系统A中的消息映射到系统B中的消息时,我需要一种映射。我希望在单个位置(单个XSLT表)定义此映射,并在多个其他XSLT表中使用它(通过incluc

我想定义一个xslt表,其中定义了变量和访问函数。然后应该在另一个样式表中使用该样式表。其主要思想是为接口映射任务定义键值对。以下代码不会产生结果

编辑:现实世界的问题是一个系统集成问题。考虑一个“类型”属性。在系统A中,“Car”类型用键“1”进行编码。在系统B中(我必须从系统a导入消息),类型“Car”用键“X”编码。示例:1辆车X。因此,当我将系统A中的消息映射到系统B中的消息时,我需要一种映射。我希望在单个位置(单个XSLT表)定义此映射,并在多个其他XSLT表中使用它(通过incluce命令)。只要使用map,我还想定义如下方便的访问函数:getTargetKey(mapWhereTheKeyIsIn,'sourceKey')。因此,无论何时,我都必须将消息从系统A映射到系统B的模式中,我想键入:
并获取
X


样本输入

<?xml version="1.0" encoding="UTF-8"?>
<order_system_a type="1" />
<?xml version="1.0" encoding="UTF-8"?>
<order_system_b type="X" />

所需输出

<?xml version="1.0" encoding="UTF-8"?>
<order_system_a type="1" />
<?xml version="1.0" encoding="UTF-8"?>
<order_system_b type="X" />

以下是请求的问题通用解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" exclude-result-prefixes="my xs">
 <xsl:import href="C:/temp/delete/GlobalTypeMappings.xsl"/>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="order_system_a">
  <order_system_b>
   <xsl:apply-templates select="@*"/>
  </order_system_b>
 </xsl:template>

 <xsl:template match="order_system_a/@type">
  <xsl:attribute name="type" select=
   "my:convertType('order_system_a', 'order_system_b', .)"/>
 </xsl:template>
</xsl:stylesheet>
<order_system_a type="1" />
<order_system_b type="X"/>

C:/temp/delete/GlobalTypeMappings.xsl:

将上述转换应用于提供的XML文档时

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" exclude-result-prefixes="my xs">
 <xsl:import href="C:/temp/delete/GlobalTypeMappings.xsl"/>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="order_system_a">
  <order_system_b>
   <xsl:apply-templates select="@*"/>
  </order_system_b>
 </xsl:template>

 <xsl:template match="order_system_a/@type">
  <xsl:attribute name="type" select=
   "my:convertType('order_system_a', 'order_system_b', .)"/>
 </xsl:template>
</xsl:stylesheet>
<order_system_a type="1" />
<order_system_b type="X"/>

生成所需的正确结果

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my" exclude-result-prefixes="my xs">
 <xsl:import href="C:/temp/delete/GlobalTypeMappings.xsl"/>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="order_system_a">
  <order_system_b>
   <xsl:apply-templates select="@*"/>
  </order_system_b>
 </xsl:template>

 <xsl:template match="order_system_a/@type">
  <xsl:attribute name="type" select=
   "my:convertType('order_system_a', 'order_system_b', .)"/>
 </xsl:template>
</xsl:stylesheet>
<order_system_a type="1" />
<order_system_b type="X"/>


您为什么需要这样做?只需将变量本身作为第一个参数传递给函数。请定义你正在解决的真正问题,然后很多人就能给出解决方案。这是很好的第一步。然而,您需要忘记提供的是XML文档(请尽量小),以及所需的结果。这似乎是一项简单的任务,当然,不需要您要求的不可能的动态变量选择。一旦你提供了缺失的数据,我会给你一个解决方案。我已经添加了一个最小的输入和输出。