Xml 使用xslt转换中的序列
我正在研究从xml到xml的xml转换。我已经为新转换的XML定义了XSD。XSD对每个元素都有一些预定义的序列/顺序。当XML转换时,如何从XSD生成相同的序列 我试图按照XSD中的顺序安排转换序列,但了解到转换序列和xslt执行序列不同 感谢您的回复Xml 使用xslt转换中的序列,xml,xsd,xslt-2.0,Xml,Xsd,Xslt 2.0,我正在研究从xml到xml的xml转换。我已经为新转换的XML定义了XSD。XSD对每个元素都有一些预定义的序列/顺序。当XML转换时,如何从XSD生成相同的序列 我试图按照XSD中的顺序安排转换序列,但了解到转换序列和xslt执行序列不同 感谢您的回复 <ROOT> <A1>A</A1> <B1>B</B1> <C1>C</C1> <D1>D</D1> </ROOT
<ROOT>
<A1>A</A1>
<B1>B</B1>
<C1>C</C1>
<D1>D</D1>
</ROOT>
<ROOT>
<a1>A</a1>
<d1>D</d1>
<b1>B</b1>
<c1>C</c1>
</ROOT>
A.
B
C
D
A.
D
B
C
根据你的建议,我在下面试了一下
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROOT">
<xsl:apply-templates select="A1,D1,C1,B1" />
</xsl:template>
<xsl:template match="A1">
<a1>
<xsl:apply-templates />
</a1>
</xsl:template>
<xsl:template match="B1">
<b1>
<xsl:apply-templates />
</b1>
</xsl:template>
<xsl:template match="C1">
<c1>
<xsl:apply-templates />
</c1>
</xsl:template>
<xsl:template match="D1">
<d1>
<xsl:apply-templates />
</d1>
</xsl:template>
</xsl:stylesheet>
XSLT处理输入文档并应用模板,因此您需要以生成正确输出的方式编写XSLT。您没有提供任何关于您的输入以及它如何映射到您想要的输出的信息,只是您有一个输出格式的模式。虽然XSLT2.0知道模式感知XSLT处理,这主要意味着验证输入和/或输出到一个模式或一组模式,但没有魔法可以确保根据模式创建输出 因此,您必须编写代码以确保获得所需的结果,包括模式定义的顺序 例如,如果您有一个输入
<foo>
<child1>...</child1>
<child2>...</child2>
</foo>
或者比如说
<xsl:template match="foo">
<bar>
<xsl:apply-templates select="child2, child1"/>
</bar>
</xsl:template>
至于您在编辑中提供的具体示例,您已接近完成,并进行了一些改进
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROOT">
<ROOT>
<xsl:apply-templates select="A1,D1,C1,B1" />
</ROOT>
</xsl:template>
<xsl:template match="A1">
<a1>
<xsl:apply-templates />
</a1>
</xsl:template>
<xsl:template match="B1">
<b1>
<xsl:apply-templates />
</b1>
</xsl:template>
<xsl:template match="C1">
<c1>
<xsl:apply-templates />
</c1>
</xsl:template>
<xsl:template match="D1">
<d1>
<xsl:apply-templates />
</d1>
</xsl:template>
</xsl:stylesheet>
还有一个XSLT2.0处理器,比如Saxon 9
<ROOT>
<a1>A</a1>
<d1>D</d1>
<c1>C</c1>
<b1>B</b1>
</ROOT>
A.
D
C
B
您已将此问题标记为xslt-1.0和xslt-2.0-您想要哪一个?xslt-2.0-更新了我的帖子。感谢Martin,我已使用您建议的输入输出和xslt更新了帖子,但它不起作用。我已编辑并提供了一个完整的样式表,用于创建您要求的输出。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROOT">
<ROOT>
<xsl:apply-templates select="A1,D1,C1,B1" />
</ROOT>
</xsl:template>
<xsl:template match="A1">
<a1>
<xsl:apply-templates />
</a1>
</xsl:template>
<xsl:template match="B1">
<b1>
<xsl:apply-templates />
</b1>
</xsl:template>
<xsl:template match="C1">
<c1>
<xsl:apply-templates />
</c1>
</xsl:template>
<xsl:template match="D1">
<d1>
<xsl:apply-templates />
</d1>
</xsl:template>
</xsl:stylesheet>
<ROOT>
<a1>A</a1>
<d1>D</d1>
<c1>C</c1>
<b1>B</b1>
</ROOT>