English 中文(简体)
将所有 XML 属性转换为元素
原标题:Convert All XML Attributes to Elements

我有一堆 xml 元素格式 像这样:

<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.example.com/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

我想变成

<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.example.com/eclipse_logos_</imgurl>
    <res>1024x1024r(iPad/iPhone)?1280x1024r(Regular)?1440x900r(Widescreen)?1920x1080r(HDTV)?1920x1200r(Widescreen)</res>
</WP>

经过一些研究后, 使用 xslt 样式表似乎是推荐的转换方式 :

<?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>

然而,我对xsl 样式表不太熟悉。 有人能带我走过应用转换的过程吗? 每次我尝试时, 我都会得到一个空白的页面。 我在我的 xml doc 上方放置了以下文字 :

<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="wallpaper.xsl"?>

并相应命名 xsl 文件。 我错过了什么吗?

最佳回答

每当你想在计算机上做任何事时,必须找到一个程序来做。如果想要对 XML 文档应用样式表,您要寻找的程序就是 XSLT 处理器。

所有现代浏览器都有 XSLT 1.0 处理器内嵌, 如果在 XML 文档中看到 < code_ lt;? xml- stypesheet? & gt; 处理指令, 它们会自动引用它。 但这对你没有多大帮助, 因为他们要做的就是在屏幕上显示输出, 您大概希望它被发送到文件 。

周围有很多 XSLT 处理器。 在几乎所有情况下, 它们都会有一个命令线界面。 如果您使用计算机上的命令线感到舒服, 这样做是一个很好的方法。 xsltproc 和 Saxon 被提到为可能的选择: 两者都是好的, 但Saxon 的优势在于它支持 XSLT 2.0, 当您想要做比这个例子更复杂的事情时, 您迟早需要这样做 。

如果您对命令行不自在, 有 GUI 应用程序 。 如果您要做大量 XML 工作, 值得投资于 XML IDE, 其中最受欢迎的是 Altova XML Spy、 Stylus Studio 和 oXygen。 我使用 oXygen 。 如果您想要更轻重量的东西, 您可以从 SourceForge 下载给 Saxon 的 GUI 前端, 名为“ 萨克森的 Kernow ” 。

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签