这是大 xml 转换的一部分。
输入 XML
<Root>
<Family>
<Entity>
<SomeElement1/>
<ClientVO>
<FirstName>Himanshu</FirstName>
<LastName>Yadav</LastName>
<ClientAddress>
<AddressLine>Somewhere In downtown</AddressLine>
<City>Chicago</City>
<State>IL</State>
</ClientAddress>
</ClientVO>
<Child2>
<Element2/>
</Child2>
<Entity>
<SomeElement1/>
<ClientVO>
<FirstName>Himanshu</FirstName>
<LastName>Yadav</LastName>
<ClientAddress>
<AddressLine>Somewhere In downtown</AddressLine>
<City>Chicago</City>
<State>IL</State>
</ClientAddress>
</ClientVO>
<Child2>
<Element222/>
</Child2>
</Entity>
</Entity>
</Family>
</Root>
输出 XML
<Response>
<EntityRoot>
<SomeElement1/>
</EntityRoot>
<ClientInformation>
<FirstName>Himanshu</FirstName>
<LastName>Yadav</LastName>
<AddressLine>Somewhere In downtown</AddressLine>
<City>Chicago</City>
<State>IL</State>
</ClientInformation>
<Child2Root>
<Element2>
</Child2Root>
<MetadataEntityRoot>
<SomeElement1/>
</MetadataEntityRoot>
<ClientInformation>
<FirstName>Himanshu</FirstName>
<LastName>Yadav</LastName>
<AddressLine>Somewhere In downtown</AddressLine>
<City>Chicago</City>
<State>IL</State>
</ClientInformation>
<Child2Root>
<Element222>
</Child2Root>
</Response>
尝试在 xslt 下面给出但不是一个可行的解决方案,因为在<ClientVO>
or下可能有更多元素<ClientAddress>
。这更像是我想避免的一对一映射。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Entity">
<EntityRoot>
<xsl:apply-templates select="@* | node()[not(self::ClientVO| self::Child2)]" />
</EntityRoot>
<xsl:apply-templates select="ClientVO| Child2" />
</xsl:template>
<xsl:template match="ClientVO">
<ClientInformation>
<FirstName><xsl:value-of select="FirstName"/></FirstName>
<LastName><xsl:value-of select="LastName"/></LastName>
<xsl:for-each select="ClientAddress">
<AddressLine><xsl:value-of select="AddressLine"/></AddressLine>
<City><xsl:value-of select="City"/></City>
<State><xsl:value-of select="State"/></State>
</xsl:for-each>
</ClientInformation>
</xsl:template>
<xsl:template match="Child2">
<Child2Root><xsl:apply-templates select="@*|node()" /></Child2Root>
</xsl:template>
</xsl:stylesheet>