gpt4 book ai didi

java - 将 xml 转换为另一个 xml 的最简单方法是什么?

转载 作者:行者123 更新时间:2023-11-29 03:21:05 24 4
gpt4 key购买 nike

我有一个如下所示的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com