【发布时间】:2011-01-19 19:33:15
【问题描述】:
我有两个 XML,一个用于定义我的应用程序对象的模板。另一个是实际对象。基本上在每个对象中只有几个值发生了变化,这就是为什么我想提供某种模板机制并应用 XSL 将它们转换为最终的。
这是一个示例对象:
<config>
<objects>
<object code="1000" name="object1">
<template name="decoration" buyCoins="60" />
</object>
</objects>
这是该对象的示例模板:
<config xmlns:template="object-template">
<templates>
<template name="decoration">
<connection type="make" />
<placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
<requirement template:coins="buyCoins"/>
<reward xp="1" />
</buyable>
<sellable>
<reward coins="1"/>
</sellable></template></templates></config>
这是我当前的 XSL:
<xsl:variable name="templates" select="document('../templates.xml')/config/templates//template" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//template">
<xsl:variable name="itemTemplate" select="."/>
<xsl:variable name="templateName" select="@name"/>
<xsl:variable name="selectedTemplate" select="$templates[@name = $templateName]/*" />
<xsl:for-each select="$selectedTemplate">
<!-- This part is only a test to get the values that I need -->
<xsl:for-each select=".//@*[namespace-uri() = 'object-template']">
<xsl:variable name="attributeName" select="name()"/>
<xsl:variable name="attributeValue" select="."/>
<xsl:variable name="finalValue" select="$itemTemplate/@*[local-name() = $attributeValue]"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="comment()"/>
我只需要执行以下操作:
- 迭代每个包含模板标签的项目
- 为每一个获取模板文档的关联模板
- 将模板内的所有内容复制到对象中 (*)
- 将项目内的属性值(现在已粘贴模板)替换为原始模板中的实际值 (**)
解释:
(*) 申请
<template name="test"><node></template>
到
<object><template name="test"></object>
变成
<object><node></object>
(**) 在上面的原始示例中,项目模板标签的 buyCoins 值应在将文本“buyCoins”发送到输出之前替换模板的值。为了便于查找并避免注册。 exp。我正在使用命名空间。所以我在 XSL 中所做的是使用正确的命名空间搜索模板内的所有属性并搜索值。 应该将值“60”而不是“buyCoins”放在硬币属性中。
我的问题是我不明白如何复制所有内容(我相信这称为身份副本)而是替换我需要的值。
任何帮助将不胜感激,谢谢!!!
更新:
当前输出为:
<config xmlns:template="item-template">
<objects>
<object code="1000" name="object1" type="decorations">
</object>
</object>
如果我说:
<xsl:copy-of select="."/>
下面:
<xsl:for-each select="$selectedTemplate">
然后我得到:
<objects>
<object code="1000" name="object1">
<connection type="make" /><placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
<requirement template:coins="buyCoins"/>
<reward xp="1" />
</buyable>
<sellable>
<reward coins="1"/>
</sellable>
</object></objects>
这是我需要的第一部分,将与其关联的模板内容放在每个输出项上。我现在在替换值时遇到问题。
XSL 中的这些树线代表我需要的数据:
<xsl:variable name="attributeName" select="name()"/>
<xsl:variable name="attributeValue" select="."/>
<xsl:variable name="finalValue" select="$itemTemplate/@*[local-name() = $attributeValue]"/>
对于此示例中的唯一项目,这是每个变量的内容: attributeName 将包含“模板:硬币” attributeValue 将包含“buyCoins” finalValue 将包含“60”
我需要将 finalValue 而不是attributeValue 放在那个attributeName 的标签中。
那时我被卡住了:(
谢谢!
更新 2:
为避免误解,这里是我需要的确切输出:
<objects>
<object code="1000" name="object1">
<connection type="make" /><placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
<requirement coins="60"/>
<reward xp="1" />
</buyable>
<sellable>
<reward coins="1"/>
</sellable>
</object></objects>
而不是属性硬币中的“buyCoins”,我需要它是“60”(输入对象文件中的值)。 此外,最好的输出还应该删除属性的命名空间模板,因为它只是 XSL 知道要修改哪些属性的标志。
【问题讨论】:
-
你错过了一些非常重要的东西:转换的确切输出。请。
-
请提供所需的输出,然后我会给出必要的解决方案。
-
感谢您的关注!我更新了问题。
-
嗯...你提供你得到的输出。但我需要你想要的输出——请你提供那个输出吗?
-
对不起,我的错误。我用该信息再次更新。
标签: xml templates xslt transformation xslt-2.0