使用XSLT展平xml层次结构 [英] Flatten xml hierarchy using XSLT

查看:89
本文介绍了使用XSLT展平xml层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用XSLT将嵌套的xml转换为扁平化的xml.

I would like to convert a nested xml to flatten xml using XSLT.

传入的xml结构类似,但是传入的xml的节点名称会更改,因此希望动态处理

Incoming xml Structure would be similar but the node names would change for the incoming xmls, so would like to handle in dynamically

样本输入

<?xml version="1.0" encoding="UTF-8"?>
<queryResponse>
  <Account>
    <Id>0010</Id>
    <Name>AA</Name>
    <RecordTypeId>0122/RecordTypeId>
    <RecordType>
      <Id>012</Id>
      <DeveloperName>Legal_Associate</DeveloperName>
    </RecordType>
  </Account>
  <Account>
    <Id>0011</Id>
    <Name>BB</Name>
    <RecordTypeId>0123</RecordTypeId>
    <RecordType>
      <Id>013</Id>
      <DeveloperName>Legal_Associate</DeveloperName>
    </RecordType>
  </Account>
 </queryResponse>

预期产量

<?xml version="1.0" encoding="UTF-8"?>
<queryResponse>
  <Account>
    <Id>0010</Id>
    <Name>AA</Name>
    <RecordTypeId>0122</RecordTypeId>
    <RecordType.Id>012</RecordType.Id>
  <RecordType.DeveloperName>Legal_Associate</RecordType.DeveloperName>
  </Account>
  <Account>
    <Id>0011</Id>
    <Name>BB</Name>
    <RecordTypeId>0123<RecordTypeId>
    <RecordType.Id>013</RecordType.Id>    <RecordType.DeveloperName>Legal_Associate</RecordType.DeveloperName>
    </Account>
    </queryResponse>

推荐答案

在发布问题时,您还应该发布已尝试的代码,以便我们可以告诉您更新.

While posting the question you should post the tried code as well hense we can tell you the updation.

这是您可以得到答案的代码:

Here is the code which you can achive your answer:

<?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="2.0">

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="RecordType">
        <xsl:for-each select="*">
            <xsl:element name="{concat(name(..),'.',name())}">
                <xsl:apply-templates select="node()"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

这篇关于使用XSLT展平xml层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆