为什么用元素替换一个属性有效,而两个属性无效? [英] Why replace one attribute by element is valid, and two attributes not?

查看:32
本文介绍了为什么用元素替换一个属性有效,而两个属性无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

身份转换中,我们可以通过

In a identity transform we can delete an attribute by

<xsl:template match="@myAttrib"/>

这适用于任何输入......我们可以用一个元素替换"属性

this works for any input... And we can "replace" the attribute by an element with

<xsl:template match="@myAttrib"><b>my new element</b></xsl:template>

...但只有当输入只有一个属性时才有效.

... but it works only when input have only one attribute.

另一方面,如果我需要替换属性的值,xsl:template 行为是相同的,即

By other hand, if I need to replace attribute's value, the xsl:template behaviour is the same, that is,

<xsl:template match="@myAttrib">newValue</xsl:template>

不替换值,而是删除属性并将newValue"作为 textNode 包含.

not replaces the value, but delete attribute and include the "newValue" as a textNode.

  • 为什么替换值"无效?
  • 为什么按元素替换"不是错误?
  • 为什么两个属性(每个元素)上下文"中的按元素替换"是错误的?

EDIT(解释两个属性"),假设输入

EDIT (for explain "two attributes"), suppose the input

<root>
    <parent myAttrib1="1" myAttrib2="2">
         <child myAttrib="1" myAttrib3="1"/>
    </parent>
    <sibling myAttrib0="1"/>
</root>

只有同级元素有一个属性.

only the element sibling have one attribute.

推荐答案

  • 为什么替换值"无效?
  • XSLT 模板将一件事替换为另一件事.在这种情况下,您将使用文本节点替换属性.如果你想用另一个同名但内容不同的属性替换一个属性,你可以这样做:

    An XSLT template replaces one thing with another thing. In this case, you are replacing an attribute with a text node. If you want to replace an attribute with another attribute of the same name, but different content, you can do this:

    <xsl:template match="@myAttrib">
        <xsl:attribute name="{name()}">newValue</xsl:attribute>
    </xsl:template>
    

    • 为什么按元素替换"不是错误?
    • 见上.XSLT 模板用一件事代替另一件事.

      See above. An XSLT template substitutes one thing for another.

      • 为什么两个属性(每个元素)上下文"中的按元素替换"是错误的?

      这在某些情况下可能会导致错误.它不会自动出错.

      This can cause an error in certain situations. It is not automatically an error.

      XSLT 不允许在已添加其他节点类型后向输出流中的父元素添加属性.据推测,您的情况发生了什么:

      XSLT does not allow adding attributes to a parent element in the output stream after other node types have already been added. Presumably, what is happening in your case is:

      1. 您正在用一个元素替换 myAttrib1.
      2. 您有一个将 myAttrib2 作为新属性复制的身份模板.
      1. You are substituting myAttrib1 with an element.
      2. You have an identity template copying myAttrib2 as a new attribute.

      如果在 myAttrib1 之后处理 myAttrib2,则会发生错误.(不保证处理属性的顺序).

      If myAttrib2 gets processed after myAttrib1, then an error will occur. (There is no guarantee on the order in which attributes will be processed).

      这可能很难解决,但这里有一种在某些情况下有效的方法:

      This can be tricky to fix, but here is one approach that will work in certain cases:

      <xsl:template match="@*[../@myAttrib]" />
      <xsl:template match="@myAttrib">
          <xsl:copy-of select="../@*[(. | current())[2]]" />
          <b>my new element</b>
      </xsl:template>
      

      这篇关于为什么用元素替换一个属性有效,而两个属性无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆