【问题标题】:Use variable to store and output attributes使用变量存储和输出属性
【发布时间】: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 由于我不想一遍又一遍地重复属性集,我希望将属性集存储在不同的变量中。我希望它能让你更清楚。
  • 这个问题可以帮到你stackoverflow.com/questions/9497703/…

标签: xslt


【解决方案1】:
<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:variableselect 属性中指定XPath 表达式——避免在其正文中复制内容。

解决方案

只需使用

<xsl:variable name="attributes_body" select="@chapter | @id"> 

这是一个完整的例子

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

     <xsl:template match="x/a">
      <xsl:variable name="vAttribs" select="@m | @n"/>

      <newEntry>
        <xsl:copy-of select="$vAttribs"/>
        <xsl:value-of select="."/>
      </newEntry>
     </xsl:template>
</xsl:stylesheet>

应用于此

<x>
 <a m="1" n="2" p="3">zzz</a>
</x>

产生

 <newEntry m="1" n="2">zzz</newEntry>

【讨论】:

  • Novtchev:属性集不会很遗憾不会飞。必须从 for-each 循环中的选定节点读取存储在属性中的值。由于属性集是顶级元素,我看不出你将如何做到这一点。以我的示例中的 @id 属性为例。它的值应该是 for-each 循环中选定的“figure”节点的 id
  • hmmm...您如何在位于列表中的“条目”节点上将其输出为 2 个不同的属性?我真的想要像&lt;entry chapter="2" id="fig6"&gt;Body fig&lt;/entry&gt;这样的输出
  • @HafLinger:只需使用&lt;xsl:copy-of select="$attributes_body"/&gt; -- ibefore 为元素指定任何其他子节点。有关示例,请参阅我的更新答案。
  • @HafLinger:你能理解和使用这个解决方案吗?我再次更新了我的答案,提供了更详细的解释。
  • 是的,它确实有效。伟大的。那么这是否意味着变量只能包含源树中的节点而我自己不能创建节点?所以当我需要像&lt;xsl:attribute name="fignum"&gt;&lt;xsl:value-of select="substring-after(@id, 'f')"/&gt;&lt;/xsl:attribute&gt; 这样的东西时,我运气不好?
【解决方案2】:

就我而言,我试图将store 一个标签attribute 转换为variable

为此,请使用此语法tag-name/@attribute-inside-tag-name

这是一个例子

<xsl:variable name="articleLanguage" select="/article/@language"/><!--the tricky part  -->
//<!--now you can use this this varialbe as you like  -->
<xsl:apply-templates select="front/article-meta/kwd-group[@language=$articleLanguage]"/>

xml 是

<article article-type="research-article" language="es" explicit-lang="es" dtd-version="1.0"> 
.....

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2021-06-08
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多