-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:文档中根元素之后的标记必须格式正确。

只需一个就可以正常工作<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 回答