-2

xslt の何が問題になっていますか?
入力 XML

<SaveData>
    <ClientInformation>
                                <FirstName>Steve</FirstName>
                                <LastName>Jobs</LastName>
                                <MiddleName/>
                                <DateOfBirth>09/18/2013</DateOfBirth>
                                <TaxIdentification>213465</TaxIdentification>
                                <ClientDetailPK>52385</ClientDetailPK>
                                <RoleTypeCT>OWN</RoleTypeCT>
                                <RoleTypeCT>IBE</RoleTypeCT>
                                <RoleTypeCT>Insured</RoleTypeCT>
                            </ClientInformation>
</SaveData>

出力XML

<ContractClientVO>  
    <ContractClientPK>-1380196094967</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>OWN</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380196094972</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380196094972</ClientRoleFK>
    <ContractClientPK/>
    <SegmentFK>-123456</SegmentFK>
  </ContractClientVO>

<ContractClientVO>  
    <ContractClientPK>-1380196094967</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>IBE</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380196094972</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380196094972</ClientRoleFK>
    <ContractClientPK/>
    <SegmentFK>-123456</SegmentFK>
  </ContractClientVO>

<ContractClientVO>  
    <ContractClientPK>-1380196094967</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>Insured</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380196094972</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380196094972</ClientRoleFK>
    <ContractClientPK/>
    <SegmentFK>-123456</SegmentFK>
  </ContractClientVO>

XSLT

 <xsl:template match="/">
        <xsl:for-each select="ClientInformation/RoleTypeCT">
        <xsl:variable name="contractClientPK" select="concat('-',timeStamp:currentTimeMillis())"/>
        <xsl:variable name="clientRolePK" select="concat('-',timeStamp:currentTimeMillis())"/>
            <ContractClientVO>
                <ContractClientPK><xsl:value-of select="$contractClientPK"/></ContractClientPK>
                <ClientRoleVO>
                    <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>
                    <ClientRolePK><xsl:value-of select="$clientRolePK"/></ClientRolePK>
                    <ClientDetailFK><xsl:value-of select="ClientDetailPK"/></ClientDetailFK>
                </ClientRoleVO>
                <ClientRoleFK><xsl:value-of select="$clientRolePK"/></ClientRoleFK>

            </ContractClientVO>
           </xsl:for-each>
</xsl:template>

しかし、私はこのエラーが発生します

[致命的なエラー] :1:692: ルート要素に続くドキュメントのマークアップは整形式でなければなりません。

1つだけでうまく機能します<RoleTypeCT/>

Groovy コード

def transformXml(xml, xslt)
    {
        def xmlAsString = groovy.xml.XmlUtil.serialize(xml)
        def w = new StringWriter()
        println XmlUtil.serialize(xml)
        println xslt
        TransformerFactory.newInstance()
                          .newTransformer( new StreamSource( new StringReader( xslt ) ) )
                          .transform( new StreamSource( new StringReader( xmlAsString ) ),
                          new StreamResult( w ) )
        return  new XmlSlurper().parseText(w.toString())
        //return w
    }

版画

<?xml version="1.0" encoding="UTF-8"?>
<ClientInformation>
  <FirstName>Steve</FirstName>
  <LastName>Jobs</LastName>
  <MiddleName/>
  <DateOfBirth>09/18/2013</DateOfBirth>
  <TaxIdentification>213465</TaxIdentification>
  <RelationshipToInsuredCT>Estate</RelationshipToInsuredCT>
  <RelationshipToEmployeeCT>Member</RelationshipToEmployeeCT>
  <SegmentFK>-123456</SegmentFK>
  <ClientDetailPK>52385</ClientDetailPK>
  <RoleTypeCT>OWN</RoleTypeCT>
  <RoleTypeCT>IBE</RoleTypeCT>
  <RoleTypeCT>Insured</RoleTypeCT>
</ClientInformation>

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:timeStamp="java://java.lang.System" exclude-result-prefixes="timeStamp">
    <xsl:template match="ClientInformation">
          <xsl:for-each select="RoleTypeCT">
              <xsl:variable name="contractClientPK" select="concat('-',timeStamp:currentTimeMillis())"/>
              <xsl:variable name="clientRolePK" select="concat('-',timeStamp:currentTimeMillis())"/>
              <ContractClientVO>
                  <ContractClientPK><xsl:value-of select="$contractClientPK"/></ContractClientPK>
                  <ClientRoleVO>
                      <RoleTypeCT><xsl:value-of select="."/></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>
                      <ClientRolePK><xsl:value-of select="$clientRolePK"/></ClientRolePK>
                      <ClientDetailFK><xsl:value-of select="../ClientDetailPK"/></ClientDetailFK>
                  </ClientRoleVO>
                  <ClientRoleFK><xsl:value-of select="$clientRolePK"/></ClientRoleFK>
              </ContractClientVO>
          </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
[Fatal Error] :1:490: The markup in the document following the root element must be well-formed.
Caught: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
4

2 に答える 2

2

ClientInfo一致するときはすでに にいますが、 では の子for-eachに移動するように求めています。ClientInfoClientInfo

select="Address"のアドレスの子に自分自身を再配置するために使用するだけですClientInfo

于 2013-09-25T19:43:31.527 に答える
1

右、与えられた:

def xml = '''<SaveData>
    <ClientInformation>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
        <ClientDetailPK>52385</ClientDetailPK>
        <RoleTypeCT>OWN</RoleTypeCT>
        <RoleTypeCT>IBE</RoleTypeCT>
        <RoleTypeCT>Insured</RoleTypeCT>
    </ClientInformation>
</SaveData>'''

そしてスタイルシート:

def xsl = '''<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:timeStamp="java://java.lang.System" exclude-result-prefixes="timeStamp">
    <xsl:template match="ClientInformation">
        <SaveData>
            <xsl:for-each select="RoleTypeCT">
                <xsl:variable name="contractClientPK" select="concat('-',timeStamp:currentTimeMillis())"/>
                <xsl:variable name="clientRolePK" select="concat('-',timeStamp:currentTimeMillis())"/>
                <ContractClientVO>
                    <ContractClientPK><xsl:value-of select="$contractClientPK"/></ContractClientPK>
                    <ClientRoleVO>
                        <RoleTypeCT><xsl:value-of select="."/></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>
                        <ClientRolePK><xsl:value-of select="$clientRolePK"/></ClientRolePK>
                        <ClientDetailFK><xsl:value-of select="../ClientDetailPK"/></ClientDetailFK>
                    </ClientRoleVO>
                    <ClientRoleFK><xsl:value-of select="$clientRolePK"/></ClientRoleFK>
                </ContractClientVO>
            </xsl:for-each>
        </SaveData>
    </xsl:template>
</xsl:stylesheet>'''

(PS: タイムスタンプは PK を取得する良い方法ではありません)

コードを実行できます:

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() )

どちらが印刷されますか:

<?xml version="1.0" encoding="UTF-8"?><SaveData>
  <ContractClientVO>
    <ContractClientPK>-1380198118176</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>OWN</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380198118176</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380198118176</ClientRoleFK>
  </ContractClientVO>
  <ContractClientVO>
    <ContractClientPK>-1380198118176</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>IBE</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380198118176</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380198118176</ClientRoleFK>
  </ContractClientVO>
  <ContractClientVO>
    <ContractClientPK>-1380198118177</ContractClientPK>
    <ClientRoleVO>
      <RoleTypeCT>Insured</RoleTypeCT>
      <ClientDetailVO>
        <FirstName>Steve</FirstName>
        <LastName>Jobs</LastName>
        <MiddleName/>
        <DateOfBirth>09/18/2013</DateOfBirth>
        <TaxIdentification>213465</TaxIdentification>
      </ClientDetailVO>
      <ClientRolePK>-1380198118177</ClientRolePK>
      <ClientDetailFK>52385</ClientDetailFK>
    </ClientRoleVO>
    <ClientRoleFK>-1380198118177</ClientRoleFK>
  </ContractClientVO>
</SaveData>

それはあなたが望んでいたものですか?

于 2013-09-26T12:23:43.403 に答える