简体   繁体   English

将XML从旧模式转换为新模式?

[英]Transform XML from old schema to new schema?

I need to transform an XML document which does not use any schema to another format which uses a well defined schema. 我需要将不使用任何模式的XML文档转换为使用定义良好的模式的另一种格式。

So basically I have to transform this: 因此,基本上我必须对此进行转换:

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

Into this: 变成这个:

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

I know how to transform it by hand by looking at the schema. 我知道如何通过查看架构来手动转换它。 Here is a snippet of the XSD: 这是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"/>

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. 我不知道如何解决:如何利用目标XSD将节点从源XML转换为目标XML文档。 I feel all the info to perform the transform is located in the XSD but can I use it? 我觉得执行转换的所有信息都位于XSD中,但是我可以使用它吗? how? 怎么样?

Any help will be greatly appreciated! 任何帮助将不胜感激!

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM