首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用for-each元素的XSLT嵌套对象

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

Stack Overflow用户
提问于 2018-04-02 16:11:55
回答 1查看 377关注 0票数 0

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

XML输入格式:

代码语言:javascript
复制
<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输出格式:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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。

EN

回答 1

Stack Overflow用户

发布于 2018-04-02 16:26:02

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

代码语言:javascript
复制
<?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中拼写出身份转换模板:

代码语言:javascript
复制
<?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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49607644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档