【问题标题】:Create attribute element with the same name as existing attribute in XSLT 2.0在 XSLT 2.0 中创建与现有属性同名的属性元素
【发布时间】:2015-04-16 10:12:45
【问题描述】:

我有一个 XML 输入,其中的元素具有许多属性。我事先不知道,哪些属性。

我想为每个现有属性创建一个同名的属性元素。

输入:

<elem id="1" name="test" version="2" />
<elem id="2" check="true" base="dir"/>

输出:

<newelem newattribute="bla" newattribute2="blabla" id="1" name="test" version="2"/>
<newelem newattribute="bla" newattribute2="blabla" id="2" check="true" base="dir"/>

我试过了:

    <xsl:for-each select="@*">
        <xsl:attribute name="name(.)" select="."/>
    </xsl:for-each>

但是 name() 函数似乎在这里不起作用。

这样做的正确方法是什么?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    有一个更简单的方法,只需使用&lt;xsl:copy-of select="@*"/&gt;

    【讨论】:

    • 啊,对,我总是忘记那些复制元素。非常感谢!
    【解决方案2】:

    把名字函数放在{}之间

     <xsl:for-each select="@*">
            <xsl:attribute name="{name(.)}" select="."/>
     </xsl:for-each>
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您可以用花括号括住对name(.) 的调用:

      <xsl:attribute name="{name(.)}" select="."/>
      

      【讨论】:

        猜你喜欢
        • 2013-11-04
        • 2023-01-26
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多