使用XSLT更改xml元素的顺序
我有一个xml输出,如下所示(请参见-'Current'),需要更改某些元素的顺序,使其显示如下(请参见-Required)。目前,一些XSLT被用于转换access DB的初始原始输出,以获得下面的“当前”示例。是否可以使用XSLT更改排序 当前使用XSLT更改xml元素的顺序,xml,xslt,Xml,Xslt,我有一个xml输出,如下所示(请参见-'Current'),需要更改某些元素的顺序,使其显示如下(请参见-Required)。目前,一些XSLT被用于转换access DB的初始原始输出,以获得下面的“当前”示例。是否可以使用XSLT更改排序 当前 <DFileUpload> <Sessions> <Session> <SessionId>ABC181_1483</SessionId> <C
<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/>
ABC181_1483
基布1
2018-01-22
1.
0
0
0
0
0
0
1.
另外
BSAC
客户
ABC181_1484
基布2
2018-01-30
1.
0
0
0
0
0
0
1.
另外
BSAC
客户
必需的
<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/>
ABC181_1483
基布1
2018-01-22
1.
0
0
0
0
BSAC
客户
0
0
1.
另外
ABC181_1484
基布2
2018-01-30
1.
0
0
0
0
BSAC
客户
0
0
1.
另外
您要进行的转换归结为 更改每个
会话中子元素的顺序
按以下方式添加元素:
- 复制除TimeMinutes、TotalCost、,
数量、主题和会话客户端
- 然后按以下顺序复制上述元素:
会话客户端、时间分钟、总成本、数量、,
和主题
所以最自然的方式是用模板匹配来表达
会话
。幸运的是,XSLT允许XPath使用except子句,
因此,模板的内容与上述内容非常接近
打开文本措辞
您还需要标识模板
因此,整个脚本可以如下所示:
<?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>
我认为最简单的方法(以任何顺序处理输入,而不一定是您示例中的顺序)是
在XSLT1.0中,不能使用“,”运算符,必须将其分解为一系列xsl:copy指令,如
<xsl:copy-of select="SessionId"/>
<xsl:copy-of select="CaseId"/>
etc.
等
(请在以后的问题中说明您使用的是哪个版本的XSLT,它可以节省每个人的时间和精力。)注意,这是XSLT 2.0。我添加了一个转换,如下所示&根据指南,谢谢,但是,它似乎排除了session元素—我要添加的内容如下:在我的解决方案中匹配session
的模板在最开始处包含
,在最末尾包含
。所以它复制了会话
元素的开始/结束标记。检查您的代码是否包含这些说明。另一种检查方法:使用在线XSLT验证器(例如xsltransform.net)。输入源XML和我的脚本,您将看到结果。嗨,Michael Kay,谢谢-XSLT中的副本在这方面很有用。如果它解决了问题,请单击答案旁边的勾号/复选标记,将答案标记为已接受。我已按照上述建议添加了使用xlst 1.0的转换。子元素已排序,但我已丢失/排除“会话”父元素。是否需要添加任何其他内容,以便仍然包含每个会话父元素?只需将所有xsl:copy of
指令包装在..
或..中即可。。。
<xsl:copy-of select="SessionId"/>
<xsl:copy-of select="CaseId"/>
etc.