0

输入 XML

<ClientInformation>
<FirstName>Ram</FirstName>
<LastName>Shah</LastName>
<MiddleName></MiddleName>
<DateOfBirth>01/01/1972</DateOfBirth>
<TaxIdentification>12346</TaxIdentification>
<RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
<RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
<RoleTypeCT>Insured</RoleTypeCT>
</ClientInformation>

输出 XML

<ContractClientVO>
 <ConstantElement1></ConstantElement1>
 <ConstantElement2></ConstantElement2>
 <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
 <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
 <ClientRoleVO>
  <ClientRoleConstantElement1></ClientRoleConstantElement1>
  <RoleTypeCT>Insured</RoleTypeCT>
  <ClientDetailVO>
   <FirstName>Ram</FirstName>
    <LastName>Shah</LastName>
    <MiddleName></MiddleName>
    <DateOfBirth>01/01/1972</DateOfBirth>
    <TaxIdentification>12346</TaxIdentification>
  </ClientDetailVO>
 <ClientRoleVO>
</ContractClientVO>

用于映射参考的 XML

<ClientInformation>
    <ContractClientVO>
     <AssignmentReasonCT></AssignmentReasonCT>
     <Associated></Associated>
     <AuthorizedSignatureCT></AuthorizedSignatureCT>
     <ClassCT></ClassCT>
     <RelationshipToInsuredCT></RelationshipToInsuredCT>
     <RelationshipToEmployeeCT></RelationshipToEmployeeCT>
     <ClientRoleVO>
      <AgentFK></AgentFK>
      <Associated></Associated>
      <RoleTypeCT></RoleTypeCT>
      <ClientDetailVO>
        <address></address>
        <city></city>
        <state></state>
        <Amount></Amount>
        <CountryTypeCT></CountryTypeCT>
        <AmountOfInsurance></AmountOfInsurance>
        <FirstName></FirstName>
        <LastName></LastName>
        <TaxIdentification></TaxIdentification>
     </ClientDetailVO>
   </ClientRoleVO>
  </ContractClientVO>
 </ClientInformation>

所以第三个xml供参考。它有助于定义输出 xml 的结构。
Groovy 是否有针对此类问题的开箱即用解决方案?或者我已经通过每个input xml元素并使用reference xml.

4

1 回答 1

1

所以,给定这个 xml:

<Clients>
    <ClientInformation>
        <FirstName>Ram</FirstName>
        <LastName>Shah</LastName>
        <MiddleName></MiddleName>
        <DateOfBirth>01/01/1972</DateOfBirth>
        <TaxIdentification>12346</TaxIdentification>
        <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
        <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
        <RoleTypeCT>Insured</RoleTypeCT>
    </ClientInformation>
</Clients>

您可以使用此 XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Output>
            <xsl:for-each select="Clients/ClientInformation">
                <ContractClientVO>
                    <ConstantElement1></ConstantElement1>
                    <ConstantElement2></ConstantElement2>
                    <RelationshipToInsuredCT><xsl:value-of select="RelationshipToInsuredCT"/></RelationshipToInsuredCT>
                    <RelationshipToEmployeeCT><xsl:value-of select="RelationshipToEmployeeCT"/></RelationshipToEmployeeCT>
                    <ClientRoleVO>
                        <ClientRoleConstantElement1></ClientRoleConstantElement1>
                        <RoleTypeCT><xsl:value-of select="RoleTypeCT"/></RoleTypeCT>
                        <ClientDetailVO>
                            <FirstName><xsl:value-of select="FirstName"/></FirstName>
                            <LastName><xsl:value-of select="LastName"/></LastName>
                            <MiddleName><xsl:value-of select="MiddleName"/></MiddleName>
                            <DateOfBirth><xsl:value-of select="DateOfBirth"/></DateOfBirth>
                            <TaxIdentification><xsl:value-of select="TaxIdentification"/></TaxIdentification>
                        </ClientDetailVO>
                    </ClientRoleVO>
                </ContractClientVO>
            </xsl:for-each>
        </Output>
    </xsl:template>
</xsl:stylesheet>

生成:

<Output>
   <ContractClientVO>
      <ConstantElement1 />
      <ConstantElement2 />
      <RelationshipToInsuredCT>Spouse</RelationshipToInsuredCT>
      <RelationshipToEmployeeCT>Spouse</RelationshipToEmployeeCT>
      <ClientRoleVO>
         <ClientRoleConstantElement1 />
         <RoleTypeCT>Insured</RoleTypeCT>
         <ClientDetailVO>
            <FirstName>Ram</FirstName>
            <LastName>Shah</LastName>
            <MiddleName />
            <DateOfBirth>01/01/1972</DateOfBirth>
            <TaxIdentification>12346</TaxIdentification>
         </ClientDetailVO>
      </ClientRoleVO>
   </ContractClientVO>
</Output>

转换 xml 的常规方法是(假设 xml 在 String 中xml,而 xsl 在 String 中xsl):

import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

def w = new StringWriter()
TransformerFactory.newInstance()
                  .newTransformer( new StreamSource( new StringReader( xsl ) ) )
                  .transform( new StreamSource( new StringReader( xml ) ),
                  new StreamResult( w ) )
println groovy.xml.XmlUtil.serialize( w.toString() )
于 2013-09-18T14:31:14.933 回答