首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将XML属性转换为元素XSLT

将XML属性转换为元素XSLT
EN

Stack Overflow用户
提问于 2012-05-18 08:47:53
回答 2查看 16.9K关注 0票数 11

我正在尝试将属性转换为子元素,即转换为以下内容:

代码语言:javascript
复制
<WP featured="yes" player="no" dancers="no" series="logos" archive="no" fanart="no" id="eclipse_logos_">
    <seriesName>LOGOS</seriesName>
    <selection>ECLIPSE</selection>
    <imgurl>http://www.nba.com/warriors/photos/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

进入:

代码语言:javascript
复制
<WP>
    <featured>yes</featured>
    <player>no</player>
    <dancers>no</dancers>
    <series>logos</series>
    <archive>no</archive>
    <fanart>no></fanart>
    <id>eclipse_logos_</id>
    <seriesName>LOGOS</seriesName>
    <selection>ECLIPSE</selection>
    <imgurl>http://www.nba.com/warriors/photos/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>
EN

回答 2

Stack Overflow用户

发布于 2012-05-18 09:07:33

试试这个:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="@*">
    <xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>
  </xsl:template>
</xsl:stylesheet>
票数 11
EN

Stack Overflow用户

发布于 2012-05-18 12:50:48

dradu的代码是通用的,转换将适用于所有属性,下面的代码更具体关于WP元素:只有那些属于WP元素的属性才会转换为元素。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="WP/@*">
    <xsl:element name="{name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10645359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档