XML文件的转换 - 将属性值和值更改为属性值

发布于 2025-02-12 23:31:27 字数 437 浏览 2 评论 0原文

我是否有一种方法可以在XML中制作“开关”,就像我在标题中在标题中更改为标签属性的属性值,并将值转换为标签属性的值。 示例:

<attribute name="Krantyp">value</attribute>
<attribute name="Beschreibung">value2</attribute>

要变成这样的东西:

<attribute Krantyp="value" Beschreibung="value2"/>

因此,在此示例中,标签是“属性”,属性名称的值必须成为属性(名称不再存在!),这里的“ krantyp”成为新属性。标签“值”的值成为新属性“ krantyp”的值,依此类推。

这甚至可能吗?如果是这样,最好的方法是什么?

Is there a way to make "switches" in XML, as I explained in title changing attribute values into attributes of tag, and values into values of attributes of a tag.
Example:

<attribute name="Krantyp">value</attribute>
<attribute name="Beschreibung">value2</attribute>

to turn into something like this:

<attribute Krantyp="value" Beschreibung="value2"/>

so, in this example, tag is "attribute", value of attribute name has to become attribute (name not exists anymore!), here "Krantyp" becomes new attribute. Value of tag "value" becomes value of new attribute "Krantyp" and so on.

Is this even possible? If it is, what would be the best way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2025-02-19 23:31:27

一探究竟。

输入XML

<root>
    <attribute name="Krantyp">value</attribute>
    <attribute name="Beschreibung">value2</attribute>
</root>

XSLT

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

    <xsl:template match="root">
        <xsl:copy>
            <attribute>
                <xsl:for-each select="attribute">
                    <xsl:attribute name="{@name}">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </xsl:for-each>
            </attribute>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出XML

<root>
  <attribute Krantyp="value" Beschreibung="value2"/>
</root>

Check it out.

Input XML

<root>
    <attribute name="Krantyp">value</attribute>
    <attribute name="Beschreibung">value2</attribute>
</root>

XSLT

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

    <xsl:template match="root">
        <xsl:copy>
            <attribute>
                <xsl:for-each select="attribute">
                    <xsl:attribute name="{@name}">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                </xsl:for-each>
            </attribute>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output XML

<root>
  <attribute Krantyp="value" Beschreibung="value2"/>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文