【发布时间】:2012-03-20 11:04:09
【问题描述】:
如果有一个很大的“xsl:choose”块,我需要在其中为不同的元素设置许多已定义的属性集。
我真的不喜欢在“选择”的每个分支中重复定义属性集。 所以我想使用一个包含这些属性的变量。 更容易维护,出错空间更小...
到目前为止我还没有能够将属性节点调出来?
我认为它们只是一个节点集,所以 copy-of 就可以了。
但这并没有给我任何输出。
这是因为属性节点不是真正的子节点吗?
但是 XSLT 1.O 不允许我直接处理它们...<xsl:copy-of select="$attributes_body/@*/> 返回错误
这是样式表片段(从原来的减少)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="list">
<xsl:for-each select="figure">
<xsl:variable name="attributes_body">
<xsl:attribute name="chapter"><xsl:value-of select="@chapter"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
</xsl:variable>
<xsl:variable name="attributes_other">
<xsl:attribute name="chapter"><xsl:value-of select="@book"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
</xsl:variable>
<xsl:choose>
<xsl:when test="ancestor::body">
<xsl:element name="entry">
<xsl:copy-of select="$attributes_body"/>
<xsl:text>Body fig</xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="entry">
<xsl:copy-of select="$attributes_other"/>
<xsl:text>other fig</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
如果这不能在 XLST 1.0 中完成,那么 2.0 是否能够做到这一点?
【问题讨论】:
-
我不太清楚你想达到什么目的。您是否希望将属性中的输入数据存储(并稍后检索)到变量中?如果是这样,输入 XML/html 是什么?而且,如果是这样,您可能只想使用 xsl:variable 而不是 xsl:template。并且您使用的嵌套 xsl:variable 是错误的。因此,请添加输入和预期输出以及方法 wrt 变量存储+检索,然后我们可以为您提供更好的建议。
-
@Maestro13 的目的是创建文档中所有“图形”元素的列表。根据各种条件,列表将包含具有预设属性集的元素。这些属性的值取决于源文档中找到的每个“图形”元素。我认为可能会让你感到困惑的是我重新发出了另一个图形元素。我已经编辑了原始样式表,因此它会发出一个具有不同名称的元素。
-
@Maestro13 由于我不想一遍又一遍地重复属性集,我希望将属性集存储在不同的变量中。我希望它能让你更清楚。
标签: xslt