XSLT-嵌套对象使用for-each元素

时间:2018-04-02 08:11:55

标签: xml xslt xpath

我想使用xslt转换将XML格式转换为另一种格式。但问题是我不知道如何使用for-each元素遍历嵌套对象。

XML输入格式:

<SecrviceRsp>
<Application>
    <ApplicationName>Application-1</ApplicationName>
    <Parent>
        <ParentName>Parent-1</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
        </Service>
    </Parent>
    <Parent>
        <ParentName>Parent-2</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
            <ChildService>
                <ChildServiceName>Child Service-1</ChildServiceName>
            </ChildService>
            <ChildService>
                <ChildServiceName>Child Service-2</ChildServiceName>
            </ChildService>
            <ChildService>
                <ChildServiceName>Child Service-3</ChildServiceName>
            </ChildService>
        </Service>
        <Service>
            <ServiceName>Service-2</ServiceName>
        </Service>
    </Parent>
    <Parent>
        <ParentName>Parent-3</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
            <ChildService>
                <ChildServiceName>Child Service-1</ChildServiceName>
            </ChildService>
        </Service>
    </Parent>
</Application>
<Application>
    <ApplicationName>Application-2</ApplicationName>
    <Parent>
        <ParentName>Parent-5</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
        </Service>
    </Parent>
    <Parent>
        <ParentName>Parent-6</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
            <ChildService>
                <ChildServiceName>Child Service-1</ChildServiceName>
            </ChildService>
            <ChildService>
                <ChildServiceName>Child Service-2</ChildServiceName>
            </ChildService>
            <ChildService>
                <ChildServiceName>Child Service-3</ChildServiceName>
            </ChildService>
        </Service>
    </Parent>
    <Parent>
        <ParentName>Parent-7</ParentName>
        <Service>
            <ServiceName>Service-1</ServiceName>
            <ChildService>
                <ChildServiceName>Child Service-1</ChildServiceName>
            </ChildService>
        </Service>
    </Parent>
</Application>

XML输出格式:

<ParentService>
<ParentName>Parent-1</ParentName>
<ServiceInfo>
        <ServiceName>Service-1</ServiceName>
    </ServiceInfo>
</ParentService>
<ParentService>
    <ParentName>Parent-2</ParentName>
    <ServiceInfo>
        <ServiceName>Service-1</ServiceName>
        <ChildServiceName>Child Service-1</ChildServiceName>
        <ChildServiceName>Child Service-2</ChildServiceName>
        <ChildServiceName>Child Service-3</ChildServiceName>
    </ServiceInfo>
    <ServiceInfo>
        <ServiceName>Service-2</ServiceName>
    </ServiceInfo>
</ParentService>
<ParentService>
    <ParentName>Parent-3</ParentName>
    <ServiceInfo>
        <ServiceName>Service-1</ServiceName>
        <ChildServiceName>Child Service-1</ChildServiceName>
    </ServiceInfo>
</ParentService>
<ParentService>
    <ParentName>Parent-5</ParentName>
    <ServiceInfo>
        <ServiceName>Service-1</ServiceName>
    </ServiceInfo>
</ParentService>
<ParentService>
    <ParentName>Parent-6</ParentName>
    <ServiceInfo>
        <ServiceName>Service-1</ServiceName>
        <ChildServiceName>Child Service-1</ChildServiceName>
        <ChildServiceName>Child Service-2</ChildServiceName>
        <ChildServiceName>Child Service-3</ChildServiceName>
    </ServiceInfo>
</ParentService>
<ParentService>
    <ParentName>Parent-7</ParentName>
    <ServiceInfo>
        <ServiceName>Service-1</ServiceName>
        <ChildServiceName>Child Service-1</ChildServiceName>
    </ServiceInfo>
</ParentService>

我试过这个xslt。但我一开始就陷入了困境。

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
    <xsl:for-each select="SecrviceRsp/Application/Parent/ParentName">           
        <ParentService>
            <ParentName>
                <xsl:value-of select="."/>
            </ParentName>
            <ServiceInfo>
                <ServiceName>
                    <xsl:value-of select="SecUserPrivQueryRsp/ApplicationInfo/ParentServiceInfo/ServiceInfo/ServiceName"/>
                </ServiceName>
            </ServiceInfo>
        </ParentService>            
    </xsl:for-each> 
</xsl:template>

请指导我完成xslt。

1 个答案:

答案 0 :(得分:0)

为要转换的元素和要跳过的模板编写模板,然后在XSLT 3中使用简单的<xsl:mode on-no-match="shallow-copy"/>复制其余模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Parent">
      <ParentService>
          <xsl:apply-templates/>
      </ParentService>
  </xsl:template>

  <xsl:template match="Service">
      <ServiceInfo>
          <xsl:apply-templates/>
      </ServiceInfo>
  </xsl:template>

  <xsl:template match="SecrviceRsp | Application | ChildService">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ApplicationName"/>


</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/3NzcBsD

或在XSLT 1或2中拼写出身份转换模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="exsl msxml"
    version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Parent">
      <ParentService>
          <xsl:apply-templates/>
      </ParentService>
  </xsl:template>

  <xsl:template match="Service">
      <ServiceInfo>
          <xsl:apply-templates/>
      </ServiceInfo>
  </xsl:template>

  <xsl:template match="SecrviceRsp | Application | ChildService">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ApplicationName"/>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/6qVRKvF