我有一个xml输出,如下所示(请参阅-“当前”),并且需要更改某些元素的顺序,以使其显示如下(请参阅-必需)。当前,一些XSLT正在用于转换访问数据库的初始原始输出,以获取下面的“当前”示例。可以使用XSLT更改顺序吗?
当前
<DFileUpload>
<Sessions>
<Session>
<SessionId>ABC181_1483</SessionId>
<CaseId>KIBB1</CaseId>
<SessionDate>2018-01-22</SessionDate>
<ServiceTypeId>1</ServiceTypeId>
<TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
<FeesCharged>0</FeesCharged>
<MoneyBusinessCommunityEducationWorkshopCode>0</MoneyBusinessCommunityEducationWorkshopCode>
<InterpreterPresent>0</InterpreterPresent>
<TimeMinutes>0</TimeMinutes>
<TotalCost>0</TotalCost>
<Quantity>1</Quantity>
<Topic>OTHER</Topic>
<SessionClients>
<SessionClient>
<ClientId>BSAC</ClientId>
<ParticipationCode>Client</ParticipationCode>
</SessionClient>
</SessionClients>
</Session>
<Session>
<SessionId>ABC181_1484</SessionId>
<CaseId>KIBB2</CaseId>
<SessionDate>2018-01-30</SessionDate>
<ServiceTypeId>1</ServiceTypeId>
<TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
<FeesCharged>0</FeesCharged>
<MoneyBusinessCommunityEducationWorkshopCode>0</MoneyBusinessCommunityEducationWorkshopCode>
<InterpreterPresent>0</InterpreterPresent>
<TimeMinutes>0</TimeMinutes>
<TotalCost>0</TotalCost>
<Quantity>1</Quantity>
<Topic>OTHER</Topic>
<SessionClients>
<SessionClient>
<ClientId>BSAC</ClientId>
<ParticipationCode>Client</ParticipationCode>
</SessionClient>
</SessionClients>
</Session>
<Sessions/>
必填
<DFileUpload>
<Sessions>
<Session>
<SessionId>ABC181_1483</SessionId>
<CaseId>KIBB1</CaseId>
<SessionDate>2018-01-22</SessionDate>
<ServiceTypeId>1</ServiceTypeId>
<TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
<FeesCharged>0</FeesCharged>
<MoneyBusinessCommunityEducationWorkshopCode>0</MoneyBusinessCommunityEducationWorkshopCode>
<InterpreterPresent>0</InterpreterPresent>
<SessionClients>
<SessionClient>
<ClientId>BSAC</ClientId>
<ParticipationCode>Client</ParticipationCode>
</SessionClient>
</SessionClients>
<TimeMinutes>0</TimeMinutes>
<TotalCost>0</TotalCost>
<Quantity>1</Quantity>
<Topic>OTHER</Topic>
</Session>
<Session>
<SessionId>ABC181_1484</SessionId>
<CaseId>KIBB2</CaseId>
<SessionDate>2018-01-30</SessionDate>
<ServiceTypeId>1</ServiceTypeId>
<TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
<FeesCharged>0</FeesCharged>
<MoneyBusinessCommunityEducationWorkshopCode>0</MoneyBusinessCommunityEducationWorkshopCode>
<InterpreterPresent>0</InterpreterPresent>
<SessionClients>
<SessionClient>
<ClientId>BSAC</ClientId>
<ParticipationCode>Client</ParticipationCode>
</SessionClient>
</SessionClients>
<TimeMinutes>0</TimeMinutes>
<TotalCost>0</TotalCost>
<Quantity>1</Quantity>
<Topic>OTHER</Topic>
</Session>
<Sessions/>
答案 0 :(得分:1)
您要进行的转换归结为
更改每个Session
中子元素的顺序
元素的方式如下:
所以最自然的方法是在模板匹配中表达这一点
Session
。幸运的是,XSLT允许带有 except 子句的XPath,
因此模板的内容与上述内容非常接近
打开文字文字。
您还需要身份模板。
因此整个脚本如下所示:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="Session">
<xsl:copy>
<xsl:apply-templates select="* except (TimeMinutes,
TotalCost, Quantity, Topic, SessionClients)"/>
<xsl:apply-templates select="SessionClients, TimeMinutes,
TotalCost, Quantity, Topic"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>
答案 1 :(得分:0)
我认为最简单的方法(按任意顺序处理输入,不一定是示例中的顺序)
<xsl:template match="Session">
<xsl:copy-of select="SessionId, CaseId, SessionDate,
ServiceTypeId, TotalNumberOfUnidentifiedClients,
FeesCharged,
MoneyBusinessCommunityEducationWorkshopCode,
InterpreterPresent, SessionClients, TimeMinutes,
TotalCost, Quantity, Topic"/>
</xsl:template>
在XSLT 1.0中,您不能使用“,”运算符,您必须将其分成一系列xsl:copy-of指令,例如
<xsl:copy-of select="SessionId"/>
<xsl:copy-of select="CaseId"/>
etc.
(请在以后的问题中说出您所使用的XSLT版本,它可以节省每个人的时间和精力。)