简体   繁体   English

使用XSLT 1.0更改XML输出结构

[英]Changing XML output structure using XSLT 1.0

I have an xml output structured as follows: 我有一个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>

The Sessions are required as a second level only once below the 'dataroot' and not required to appear with each and every 'Session'. 会话仅在“数据根”下面需要作为第二级,并且不需要与每个“会话”一起出现。 I am wondering what XSLT 1.0 would be required to transform this? 我想知道要转换此格式需要什么XSLT 1.0?

In XSLT 1.0 you can try this 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>

You can see conversion at https://xsltfiddle.liberty-development.net/94hvTzK 您可以在https://xsltfiddle.liberty-development.net/94hvTzK上看到转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM