gpt4 book ai didi

java - 从架构 xsd 中删除引用

转载 作者:行者123 更新时间:2023-12-01 12:27:08 24 4
gpt4 key购买 nike

有人知道如何转换 xsd 字符串包含对没有“ref”属性的字符串的引用吗?

例如我有架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pref="http://someaddr.com" elementFormDefault="qualified" targetNamespace="http://otheraddr.com">
<xs:element name="rootElem">
<xs:complexType>
<xs:sequence>
<xs:element ref="pref:elem1"/>
<xs:element ref="pref:elem2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="elem1" type="xs:string"/>
<xs:element name="elem2" type="xs:string"/>
</xs:schema>

我想将其转换为:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pref="http://someaddr.com" elementFormDefault="qualified" targetNamespace="http://otheraddr.com">
<xs:element name="rootElem">
<xs:complexType>
<xs:sequence>
<xs:element name="elem1" type="xs:string"/>
<xs:element name="elem2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

我正在寻找一些功能,例如 xerces libs,但我找不到任何东西。而且我自己用 Java 编写它也会遇到很多困难。

最佳答案

描述此转换的 XSLT 样式表可能如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

<!-- transform all attributes and nodes as themselves,
except where a more selective rule applies -->
<xsl:template match="@*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>

<!-- transform xs:element elements with ref attributes
as a copy of the referenced schema-level xs:element -->
<xsl:template match="xs:element[@ref]">
<xsl:variable name="ref-name" select="@ref" as="xs:string" />
<!-- the mode specified below doesn't matter; it just
mustn't be the default mode -->
<xsl:apply-templates
select="/xs:schema/xs:element[@name=$ref-name]"
mode="inline" />
</xsl:template>

<!-- in the default mode, transform schema-level
xs:element elements to nothing -->
<xsl:template match="/xs::schema/xs:element[@name]" />

</xsl:stylesheet>

请注意,无论实现如何,请求的操作都可能需要无限递归。

关于java - 从架构 xsd 中删除引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26243335/

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