首页 > 解决方案 > XML - 使用 XSLT 根据 XSD 模式按顺序重新排列节点

问题描述

我正在尝试根据 XSD 架构安排 xml 节点顺序,但它没有按预期工作。这里我试图根据 xsd 模式将标签放入 xml 中。这是标签之一。如果 xml 以任何顺序作为输入需要按照预期输出的模式顺序重新排列,我需要放置请帮助我解决。

如果我想更改架构以允许元素,以便我如何更改 xsd 架构中“序列”中的元素。

输入 XML

<Location>
  <identifier>
    <value value="Ide123" />
  </identifier>
  <managingOrganization>
    <reference value="Test/vaaa" />
  </managingOrganization>
  <name value="Test name" />
 </Location>

预期输出 XML:

<Location>
  <identifier>
    <value value="Ide123" />
  </identifier>
  <name value="Test name" />
  <managingOrganization>
    <reference value="Test/vaaa" />
  </managingOrganization>
 </Location>

输入 XSD 架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://hl7.org/fhir" xmlns:xhtml="http://www.w3.org/1999/xhtml" elementFormDefault="qualified" version="1.0">
  <xs:include schemaLocation="fhir-base.xsd"/>
  <xs:element name="Location" type="Location">
    <xs:annotation>
      <xs:documentation xml:lang="en">Details and position information for a physical place where services are provided  and resources and participants may be stored, found, contained or accommodated.</xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:complexType name="Location">
    <xs:annotation>
      <xs:documentation xml:lang="en">Details and position information for a physical place where services are provided  and resources and participants may be stored, found, contained or accommodated.</xs:documentation>
      <xs:documentation xml:lang="en">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="DomainResource">
        <xs:sequence>
          <xs:element name="identifier" minOccurs="0" maxOccurs="unbounded" type="Identifier">
            <xs:annotation>
              <xs:documentation xml:lang="en">Unique code or number identifying the location to its users.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="status" minOccurs="0" maxOccurs="1" type="LocationStatus">
            <xs:annotation>
              <xs:documentation xml:lang="en">The status property covers the general availability of the resource, not the current value which may be covered by the operationStatus, or by a schedule/slots if they are configured for the location.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="operationalStatus" minOccurs="0" maxOccurs="1" type="Coding">
            <xs:annotation>
              <xs:documentation xml:lang="en">The Operational status covers operation values most relevant to beds (but can also apply to rooms/units/chair/etc such as an isolation unit/dialisys chair). This typically covers concepts such as contamination, housekeeping and other activities like maintenance.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="name" minOccurs="0" maxOccurs="1" type="string">
            <xs:annotation>
              <xs:documentation xml:lang="en">Name of the location as used by humans. Does not need to be unique.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="alias" minOccurs="0" maxOccurs="unbounded" type="string">
            <xs:annotation>
              <xs:documentation xml:lang="en">A list of alternate names that the location is known as, or was known as in the past.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="description" minOccurs="0" maxOccurs="1" type="string">
            <xs:annotation>
              <xs:documentation xml:lang="en">Description of the Location, which helps in finding or referencing the place.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="mode" minOccurs="0" maxOccurs="1" type="LocationMode">
            <xs:annotation>
              <xs:documentation xml:lang="en">Indicates whether a resource instance represents a specific location or a class of locations.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="type" minOccurs="0" maxOccurs="1" type="CodeableConcept">
            <xs:annotation>
              <xs:documentation xml:lang="en">Indicates the type of function performed at the location.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="telecom" minOccurs="0" maxOccurs="unbounded" type="ContactPoint">
            <xs:annotation>
              <xs:documentation xml:lang="en">The contact details of communication devices available at the location. This can include phone numbers, fax numbers, mobile numbers, email addresses and web sites.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="address" minOccurs="0" maxOccurs="1" type="Address">
            <xs:annotation>
              <xs:documentation xml:lang="en">Physical location.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="physicalType" minOccurs="0" maxOccurs="1" type="CodeableConcept">
            <xs:annotation>
              <xs:documentation xml:lang="en">Physical form of the location, e.g. building, room, vehicle, road.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="position" type="Location.Position" minOccurs="0" maxOccurs="1">
            <xs:annotation>
              <xs:documentation xml:lang="en">The absolute geographic location of the Location, expressed using the WGS84 datum (This is the same co-ordinate system used in KML).</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="managingOrganization" minOccurs="0" maxOccurs="1" type="Reference">
            <xs:annotation>
              <xs:documentation xml:lang="en">The organization responsible for the provisioning and upkeep of the location.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="partOf" minOccurs="0" maxOccurs="1" type="Reference">
            <xs:annotation>
              <xs:documentation xml:lang="en">Another Location which this Location is physically part of.</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="endpoint" minOccurs="0" maxOccurs="unbounded" type="Reference">
            <xs:annotation>
              <xs:documentation xml:lang="en">Technical endpoints providing access to services operated for the location.</xs:documentation>
           </xs:annotation>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="Location.Position">
    <xs:annotation>
      <xs:documentation xml:lang="en">Details and position information for a physical place where services are provided  and resources and participants may be stored, found, contained or accommodated.</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="BackboneElement">
        <xs:sequence>
          <xs:element name="longitude" minOccurs="1" maxOccurs="1" type="decimal">
            <xs:annotation>
              <xs:documentation xml:lang="en">Longitude. The value domain and the interpretation are the same as for the text of the longitude element in KML (see notes below).</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="latitude" minOccurs="1" maxOccurs="1" type="decimal">
            <xs:annotation>
              <xs:documentation xml:lang="en">Latitude. The value domain and the interpretation are the same as for the text of the latitude element in KML (see notes below).</xs:documentation>
           </xs:annotation>
          </xs:element>
          <xs:element name="altitude" minOccurs="0" maxOccurs="1" type="decimal">
            <xs:annotation>
              <xs:documentation xml:lang="en">Altitude. The value domain and the interpretation are the same as for the text of the altitude element in KML (see notes below).</xs:documentation>
           </xs:annotation>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:simpleType name="LocationMode-list">
    <xs:restriction base="code-primitive">
      <xs:enumeration value="instance">
        <xs:annotation>
          <xs:documentation xml:lang="en">Instance</xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="kind">
        <xs:annotation>
          <xs:documentation xml:lang="en">Kind</xs:documentation>
        </xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="LocationMode">
    <xs:annotation>
      <xs:documentation xml:lang="en">Indicates whether a resource instance represents a specific location or a class of locations.</xs:documentation>
      <xs:documentation xml:lang="en">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="Element">
        <xs:attribute name="value" type="LocationMode-list" use="optional"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:simpleType name="LocationStatus-list">
    <xs:restriction base="code-primitive">
      <xs:enumeration value="active">
        <xs:annotation>
          <xs:documentation xml:lang="en">Active</xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="suspended">
        <xs:annotation>
          <xs:documentation xml:lang="en">Suspended</xs:documentation>
        </xs:annotation>
      </xs:enumeration>
      <xs:enumeration value="inactive">
        <xs:annotation>
          <xs:documentation xml:lang="en">Inactive</xs:documentation>
        </xs:annotation>
      </xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="LocationStatus">
    <xs:annotation>
      <xs:documentation xml:lang="en">Indicates whether the location is still in use.</xs:documentation>
      <xs:documentation xml:lang="en">If the element is present, it must have either a @value, an @id, or extensions</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="Element">
        <xs:attribute name="value" type="LocationStatus-list" use="optional"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

XSLT 代码

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="xml" indent="yes" />

    <xsl:key name="kxsElemByName" match="xs:element" use="@name"/>
    <xsl:key name="kxsTypeByName" match="xs:complexType" use="@name"/>

    <xsl:variable name="vSchema" select="document('file:///C:/Users/310271854/Amina/location.xsd')"/>

    <xsl:template match="*[descendant::text() or descendant-or-self::*/@*[string()]]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:variable name="current" select="." />
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
            <xsl:for-each select="$vSchema">
                <xsl:variable name="element" select="key('kxsElemByName', name($current))" />
                <xsl:variable name="complexContent" select="key('kxsTypeByName', $element/@name)" />
                <xsl:choose>
                <xsl:when test="$complexContent">
                    <xsl:for-each select="$complexContent/xs:extension/xs:sequence/xs:element">
                        <xsl:apply-templates select="$current/*[name() = current()/@name]" />
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="$current/node()"/>
                </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

标签: xmlxsltxsd

解决方案


推荐阅读