- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的 xml 文档:
<document>
<primitive name="password" type="xs:string" path="" mandatory="false" />
<structure name="username" mandatory="true">
<primitive name="first-name" type="xs:string" path="" mandatory="true" />
<primitive name="last-name" type="xs:string" path="" mandatory="true" />
</structure>
<list name="addresses" path="" mandatory="true">
<structure name="address" mandatory="true">
<primitive name="country" type="xs:string" path="" mandatory="true" default-value="HU"/>
<primitive name="po-box" type="xs:string" path="" mandatory="true" />
</structure>
</list>
<list name="owned-phones" path="" mandatory="true">
<structure name="phone" mandatory="true">
<primitive name="number" type="xs:int" path="" mandatory="true" />
<primitive name="device-name" type="xs:string" path="" mandatory="true" />
<list name="similar-devices" path="" mandatory="true">
<structure name="device" mandatory="true">
<primitive name="device-name" type="xs:string" path="" mandatory="true" />
</structure>
</list>
</structure>
</list>
<list name="aliases" path="" mandatory="true">
<primitive name="alias" type="xs:string" path="" mandatory="true" />
</list>
<template name="tou">
<![CDATA[
wombat
]]>
</template>
这描述了如下所示的数据结构规则:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<password>ajshakjsdh</password>
<username>
<first-name>Kevin</first-name>
<last-name>Smith</last-name>
</username>
<addresses>
<address>
<country>HU</country>
<po-box>12234</po-box>
</address>
<address>
<country>US</country>
<po-box>666</po-box>
</address>
</addresses>
<owned-phones>
<phone>
<number>2431241</number>
<device-name>Nokia</device-name>
<similar-devices>
<device>
<device-name>Windozfon</device-name>
</device>
</similar-devices>
</phone>
<phone>
<number>68741</number>
<device-name>Samsung</device-name>
<similar-devices>
<device>
<device-name>Android</device-name>
</device>
<device>
<device-name>Blackberry</device-name>
</device>
</similar-devices>
</phone>
</owned-phones>
<aliases>
<alias>Alias1</alias>
<alias>Alias2</alias>
</aliases>
<!-- tou is missing but not mandatory -->
</document>
我有一组转换规则,可用于将第一个 xml 文件转换为可用于验证数据结构的 xsd 文档(第二个代码块):
<primitive name="$primitive_name" type="$primitive_type" mandatory="$primitive_mandatory" />
转换为
<xs:element name="$primitive_name" type="$primitive_type" minOccurs="$primitive_mandatory ? 1 : 0" maxOccurs="1" />
<structure name="$structure_name" mandatory="$structure_mandatory">
<!-- anything inside recursively transformed -->
</structure>
转换为
<xs:element name="$structure_name" minOccurs="$structure_mandatory ? 1 : 0" maxOccurs="1">
<xs:complexType>
<xs:all>
<!-- anything inside recursively transformed -->
</xs:all>
</xs:complexType>
</xs:element>
<list name="$list_name" mandatory="$list_mandatory">
<!-- anything inside recursively transformed -->
</list>
转换为
<xs:element name="$list_name" minOccurs="$list_mandatory ? 1 : 0">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<!-- anything inside recursively transformed -->
</xs:sequence>
</xs:complexType>
</xs:element>
我的问题是我应该使用什么工具来进行这些简单的转换?我听说 XSLT 是实现这一目标的方式。我对此做了一些研究,这对我来说似乎太过分了。至少我无法在 XSLT 中找到正确的转换规则。你能提供一些我应该从哪里开始的指示吗?
最佳答案
XSLT 是解决您的问题的好方法。您可以轻松地将您的结构映射到将被递归调用的单个模板。
这是一个 XSLT 2.0 样式表的示例,其中仅包含您提供的示例的模板。它应该提供一个良好的起点。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="document">
<xs:schema elementFormDefault="qualified">
<xsl:apply-templates/>
</xs:schema>
</xsl:template>
<xsl:template match="primitive">
<xs:element name="{@name}" type="{@type}" minOccurs="{if(@mandatory eq 'true') then 1 else 0}" maxOccurs="1" />
</xsl:template>
<xsl:template match="structure">
<xs:element name="{@name}" minOccurs="{if(@mandatory eq 'true') then 1 else 0}" maxOccurs="1">
<xs:complexType>
<xs:all>
<xsl:apply-templates/>
</xs:all>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="list">
<xs:element name="{@name}" minOccurs="{if(@mandatory eq 'true') then 1 else 0}">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xsl:apply-templates/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
</xsl:stylesheet>
有很多方法可以达到相同的效果。寻找有效使用 XSLT 模板的优秀 XSLT 教程。避免使用 for-each
专注于单模板解决方案的那些.您可以使用模板实现所需的一切(不使用 <xsl:for-each>
会使它变得过于复杂)。
如果您的支持仅限于XSLT 1.0,您将无法使用minOccurs
中的(XPath 2.0) 代码。属性,但您可以使用嵌套的 <xsl:attribute>
生成属性并使用 <xsl:choose>
计算其内容。
上面的样式表可以在 XSLT 1.0 中重写如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="document">
<xs:schema elementFormDefault="qualified">
<xsl:apply-templates/>
</xs:schema>
</xsl:template>
<xsl:template match="primitive">
<xs:element name="{@name}" type="{@type}" maxOccurs="1">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="@mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xs:element>
</xsl:template>
<xsl:template match="structure">
<xs:element name="{@name}" maxOccurs="1">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="@mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xs:complexType>
<xs:all>
<xsl:apply-templates/>
</xs:all>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="list">
<xs:element name="{@name}">
<xsl:attribute name="minOccurs">
<xsl:choose>
<xsl:when test="@mandatory = 'true'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xsl:apply-templates/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
</xsl:stylesheet>
关于java - 将 xml 转换为另一个 xml 的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23612315/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!