使用XSLT 1.0更改XML输出结构

问题描述 投票:0回答:1

我有一个xml输出结构如下:

<dataroot>
  <Sessions>
     <Session>
        <SessionId>LLWNA181_3</SessionId>
        <CaseId>KIFLLW</CaseId>
        <SessionDate>2018-07-05T00:00:00</SessionDate>
        <ServiceTypeId>13</ServiceTypeId>
        <TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
        <SessionClients>
            <SessionClient>
                <ClientId>LLWNA</ClientId>
                <ParticipationCode>Client</ParticipationCode>
            </SessionClient>
        </SessionClients>
      </Session>
   </Sessions>
      <Sessions>
       <Session>
        <SessionId>LLWNA181_4</SessionId>
        <CaseId>KIFLLW</CaseId>
        <SessionDate>2018-07-05T00:00:00</SessionDate>
        <ServiceTypeId>8</ServiceTypeId>
        <TotalNumberOfUnidentifiedClients>0</TotalNumberOfUnidentifiedClients>
        <SessionClients>
            <SessionClient>
                <ClientId>LLWNA</ClientId>
                <ParticipationCode>Client</ParticipationCode>
            </SessionClient>
        </SessionClients>
     </Session>
  </Sessions>
</dataroot>

会话只需要在'dataroot'之下仅作为第二级,并且不需要与每个'Session'一起出现。我想知道转换它需要什么XSLT 1.0?

xml xslt-1.0
1个回答
0
投票

在XSLT 1.0中,您可以尝试这个

    <?xml version="1.0" encoding="UTF-8"?>
<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="Sessions[preceding-sibling::*[1][self::Sessions]]"/>

    <xsl:template match="Sessions">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        <xsl:if test="following-sibling::*[1][self::Sessions]">
            <xsl:call-template name="wrapdata">
                <xsl:with-param name="data" select="following-sibling::*[1][self::Sessions]"/>
            </xsl:call-template>
        </xsl:if>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="wrapdata">
        <xsl:param name="data"/>
        <xsl:copy-of select="$data/*"/>
        <xsl:if test="$data/following-sibling::*[1][self::Sessions]">
            <xsl:call-template name="wrapdata">
                <xsl:with-param name="data" select="$data/following-sibling::*[1][self::Sessions]"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

你可以在https://xsltfiddle.liberty-development.net/94hvTzK看到转换

© www.soinside.com 2019 - 2024. All rights reserved.