将 XML 从旧模式转换为新模式? [英] Transform XML from old schema to new schema?

查看:27
本文介绍了将 XML 从旧模式转换为新模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将不使用任何架构的 XML 文档转换为使用定义明确的架构的另一种格式.

I need to transform an XML document which does not use any schema to another format which uses a well defined schema.

所以基本上我必须改变这个:

So basically I have to transform this:

<healthCareFacilityTypeCode 
     displayName="Home" 
     codingScheme="Connect-a-thon healthcareFacilityTypeCodes"
    >Home</healthCareFacilityTypeCode>

进入这个:

<healthCareFacilityTypeCode>
    <code>Home</code>
    <displayName>
        <LocalizedString value="Home" />
    </displayName>
    <schemeName>Connect-a-thon healthcareFacilityTypeCodes</schemeName>
</healthCareFacilityTypeCode>

我知道如何通过查看架构来手动转换它.这是 XSD 的片段:

I know how to transform it by hand by looking at the schema. Here is a snippet of the XSD:

<xsd:complexType name="DocumentEntryType">
    <xsd:sequence>
        <xsd:element minOccurs="0" 
                     name="healthCareFacilityTypeCode" 
                     type="tns:CodedMetadataType"/>
    </xsd:sequence>
    <xsd:attribute default="false" 
                   name="existing" 
                   type="xsd:boolean" 
                   use="optional"/>
</xsd:complexType>
<xsd:element name="DocumentEntry" type="tns:DocumentEntryType"/>

我不知道如何解决的是:如何利用目标 XSD 将节点从源 XML 转换为目标 XML 文档.我觉得执行转换的所有信息都位于 XSD 中,但我可以使用它吗?怎么样?

What I don't know how to tackle is: how to exploit the target XSD to transform a node from the source XML to the target XML doc. I feel all the info to perform the transform is located in the XSD but can I use it? how?

任何帮助将不胜感激!

推荐答案

按照建议,这就是我想出的.不完美,但足以达到我的目的.

Followed suggestions and this is what I came up with. Not perfect but it is enough for my purpose.

    <xsl:template match="XDSDocumentEntry">
        <DocumentEntryType>
            <xsl:call-template name="namespaceChange"/>
            <xsl:apply-templates/>
        </DocumentEntryType>
    </xsl:template>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//*[matches(name(), 'Code')]">
        <xsl:copy>
            <code>
                <xsl:value-of select="."/>
            </code>
            <schemeName>
                <xsl:value-of select="@codingScheme"/>
            </schemeName>
            <displayName>
                <LocalizedString>
                    <xsl:attribute name="value">
                        <xsl:value-of select="@displayName"/>
                    </xsl:attribute>
                </LocalizedString>
            </displayName>
        </xsl:copy>
    </xsl:template>

这篇关于将 XML 从旧模式转换为新模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆