使用 xslt 进行转换的序列 [英] sequence in transformation using xslt

查看:32
本文介绍了使用 xslt 进行转换的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究从 XML 到 XML 的 xml 转换.我为新转换的 XML 定义了 XSD.XSD 对每个元素都有一些预定义的序列/顺序.如何在 XML 转换时从 XSD 显示相同的序列?

I am working on xml transformation from XML to XML. I have XSD defined for newly transformed XML. XSD has some predefined sequence/order for each element. How can I make same sequence from XSD will appears while XML transformation?

我尝试按照与 XSD 中相同的顺序排列转换顺序,但了解到转换顺序与 xslt 执行顺序不同.

I tried to arrange transformation sequence as same order as in my XSD but learnt that transformation sequence is not same as xslt execution sequence.

感谢您的回复

<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>

我根据你的建议尝试了以下

I tried below based on your suggestion

<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.您没有提供有关您拥有的输入以及它如何映射到您想要的输出的任何信息,只是您有输出格式的架构.虽然 XSLT 2.0 知道模式感知 XSLT 处理主要意味着验证模式或模式集的输入和/或输出,但没有魔法可以确保根据模式创建输出.

XSLT processes your input document and applies your templates so you need to write your XSLT in a way that it produces the proper output. You have not provided any information about the input you have and how it maps to the output you want, only that you have a schema for the output format. While XSLT 2.0 knows schema-aware XSLT processing that mainly means validating input and/or output to a schema or set of schemas, there is no magic to ensure the output is created according to a schema.

因此,您必须编写代码以确保获得所需的结果,包括您正在查找的顺序以及架构定义的顺序.

So you will have to write your code to ensure you get the result you want, including the order your are looking for respectively the schema defines.

例如如果你有一个输入

<foo>
  <child1>...</child1>
  <child2>...</child2>
</foo>

并且您想创建

<bar>
  <child2>...</child2>
  <child1>...</child1>
</bar>

然后你将foo的模板写成

<xsl:template match="foo">
  <bar>
    <xsl:apply-templates>
      <xsl:sort select="position()" order="descending"/>
    </xsl:apply-templates>
  </bar>
</xsl:template>

或者例如

<xsl:template match="foo">
  <bar>
    <xsl:apply-templates select="child2, child1"/>
  </bar>
</xsl:template>

至于您在编辑中提供的具体示例,您几乎已经完成了,但有一些改进

As for the concrete samples you have provided in an edit, you are nearly there, with some improvements

<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>

和像 Saxon 9 这样的 XSLT 2.0 处理器

and an XSLT 2.0 processor like Saxon 9 I get

<ROOT>
   <a1>A</a1>
   <d1>D</d1>
   <c1>C</c1>
   <b1>B</b1>
</ROOT>

这篇关于使用 xslt 进行转换的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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