[英]Can you transform unordered xml to match an xsd:sequence order?
Hi i need to transform unorderd xml using xslt to the correct order as specified in an xsd schema 您好我需要使用xslt将无序的xml转换为xsd架构中指定的正确顺序
<Person>
<property name="address" value="5" />
<property name="firstname" value="1234567890" />
<property name="lastname" value="The BFG" />
</Person>
would need to be transformed using 需要转换使用
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The xml could have the properties in any order, with upwars of 10+ property elements.I have tried using an xsl:for-each to try and process the xml but i'm stumped at how to get the xslt to transform the xml into the correct order as defined by the sequence xml可以有任何顺序的属性,有10多个属性元素的upwars。我已经尝试使用xsl:for-each来尝试和处理xml但是我很难理解如何让xslt将xml转换为序列定义的正确顺序
any help would be appreciated 任何帮助,将不胜感激
This may not be the best way, but it seems to work ok. 这可能不是最好的方法,但似乎工作正常。 I'm not sure if the order that the
xs:element
's are processed is guaranteed though. 我不确定是否可以保证处理
xs:element
的顺序。 Also, this is an XSLT 2.0 answer tested with Saxon-HE 9.3.0.5 in oXygen. 此外,这是使用Saxon-HE 9.3.0.5在oXygen中测试的XSLT 2.0答案。
XML Input (modified the case of Person
to match the schema): XML输入(修改
Person
的情况以匹配模式):
<person>
<property name="address" value="5" />
<property name="firstname" value="1234567890" />
<property name="lastname" value="The BFG" />
</person>
External XSD Schema file (schema.xsd): 外部XSD架构文件(schema.xsd):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSLT 2.0 Stylesheet: XSLT 2.0样式表:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="input">
<xsl:copy-of select="/"/>
</xsl:variable>
<xsl:template match="/*">
<xsl:variable name="firstContext" select="name()"/>
<xsl:variable name="xsdElems" select="document('schema.xsd')/xs:schema/xs:element[@name=$firstContext]/xs:complexType/xs:sequence/xs:element/@name"/>
<xsl:element name="{$firstContext}">
<xsl:for-each select="$xsdElems">
<xsl:variable name="secondContext" select="."/>
<xsl:element name="{$secondContext}">
<xsl:value-of select="$input/*/*[@name=$secondContext]/@value"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XML Output: XML输出:
<person>
<firstname>1234567890</firstname>
<lastname>The BFG</lastname>
<address>5</address>
</person>
Hope this helps. 希望这可以帮助。
Here is an XSLT 1.0 solution : 这是一个XSLT 1.0解决方案 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kxsElemByName" match="xs:element" use="@name"/>
<xsl:variable name="vSchema" select=
"document('file:///c:/temp/delete/schema.xsd')"/>
<xsl:variable name="vDoc" select="/"/>
<xsl:template match="/*">
<xsl:variable name="vElem" select="."/>
<xsl:for-each select="$vSchema">
<xsl:apply-templates select=
"key('kxsElemByName', name($vElem))">
<xsl:with-param name="pElement" select="$vElem"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="xs:element">
<xsl:param name="pElement"/>
<xsl:element name="{name($pElement)}">
<xsl:apply-templates mode="generate"
select="xs:complexType/xs:sequence/*">
<xsl:with-param name="pParent" select="$pElement"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="xs:element" mode="generate">
<xsl:param name="pParent"/>
<xsl:variable name="vProp" select=
"$pParent/property[@name = current()/@name]"/>
<xsl:element name="{$vProp/@name}">
<xsl:value-of select="$vProp/@value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document ( Person
renamed to person
to match the schema): 当此转换应用于提供的XML文档时 (
Person
重命名为person
以匹配架构):
<person>
<property name="address" value="5" />
<property name="firstname" value="1234567890" />
<property name="lastname" value="The BFG" />
</person>
and if the provided XML schema is in the file c:\\temp\\delete\\schema.xsd
: 如果提供的XML模式位于文件
c:\\temp\\delete\\schema.xsd
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
then the wanted, correct result is produced : 然后产生想要的,正确的结果 :
<person>
<firstname>1234567890</firstname>
<lastname>The BFG</lastname>
<address>5</address>
</person>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.