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



我想使用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。

为要转换的那些元素和要跳过的那些模板写模板,然后使用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

最新更新