我有一个需要解析的复杂xml文件。
我需要的主要信息是LeaseFiles
标签之间 - 每个人都没有所有节点,只有他们有数据......
我试图生成一个带有标题的csv文件,这些标题会使每一行的字段数相同,如果它不在xml文件中,则会将值留空。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="delimiter" select="'|'" />
<!-- define an array containing the fields we are interested in -->
<xsl:variable name="fieldArray">
<field>TotalProperties</field>
<field>TotalLeaseFiles</field>
<field>TotalTenants</field>
<field>TotalOpenAmount</field>
</xsl:variable>
<xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" />
<xsl:template match="/">
<!-- output the header row -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<!-- output newline -->
<xsl:text>
</xsl:text>
<xsl:apply-templates select="ResMan/Response/MITS-Collections/Summary"/>
</xsl:template>
<xsl:template match="Summary">
<xsl:variable name="currNode" select="." />
<!-- output the data row -->
<!-- loop over the field names and find the value of each one in the xml -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:value-of select="$currNode/*[name() = current()]" />
</xsl:for-each>
<!-- output newline -->
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
这是XML文件
<MITS-Collections xsi:noNamespaceSchemaLocation="Itf_MITS_Collections3.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
<Summary>
<MITSDocVersion />
<GenerationTimeStamp>2017-11-03T13:11:56</GenerationTimeStamp>
<SourceOrganization>23</SourceOrganization>
<TotalProperties>1</TotalProperties>
<TotalLeaseFiles>11</TotalLeaseFiles>
<TotalTenants>12</TotalTenants>
<TotalOpenAmount>3325.00</TotalOpenAmount>
</Summary>
<PropertyFiles>
<PropertyFile>
<Property>
<Identification>
<IDValue>47-550</IDValue>
</Identification>
<MarketingName />
<LegalName />
<Address AddressType="property">
<Description>PropertyAddress</Description>
<Address>Lofts </Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30324</PostalCode>
</Address>
</Property>
<LeaseFiles>
<LeaseFile>
<Identification>
<IDValue>t0143556</IDValue>
<OrganizationName>Primary Tenant Code</OrganizationName>
</Identification>
<Identification>
<IDValue>206</IDValue>
<OrganizationName>Unit Code</OrganizationName>
</Identification>
<CollectionStatus>true</CollectionStatus>
<CollectionStatusLastChangeDate>2017-11- 03</CollectionStatusLastChangeDate>
<MoveInDate>2017-05-15</MoveInDate>
<LeaseBegin>2017-05-15</LeaseBegin>
<LeaseEnd>2018-08-14</LeaseEnd>
<MonthlyRentAmount>1420.00</MonthlyRentAmount>
<Tenants>
<PersonDetails>
<Identification>
<IDValue>t0143556</IDValue>
</Identification>
<Name>
<FirstName>Octavia</FirstName>
<LastName>Smith</LastName>
</Name>
<Address AddressType="current">
<Description>current</Description>
<Address>5832 Hunt Club Run</Address>
<City>Norcross</City>
<State>GA</State>
<PostalCode>30093</PostalCode>
</Address>
<Address AddressType="current">
<Description>current</Description>
<Address>2430 Cheshire Bridge Road</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30324</PostalCode>
</Address>
<Address AddressType="previous">
<Description>previous</Description>
<Address>5832 Hunt Club Run</Address>
<City>Norcross</City>
<State>GA</State>
<PostalCode>30093</PostalCode>
</Address>
<Address AddressType="previous">
<Description>previous</Description>
<Address>118 Collier Rd Nw</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30318</PostalCode>
</Address>
<Address AddressType="other">
<Email>octaviar@gmail.com</Email>
</Address>
<Phone PhoneType="cell">
<PhoneDescription>Mobile</PhoneDescription>
<PhoneNumber>4049999000</PhoneNumber>
</Phone>
</PersonDetails>
<DateOfBirth>1996-01-01</DateOfBirth>
<SSN>199-99-9999</SSN>
<DriversLicense>GA 058</DriversLicense>
<Income>
<IncomeSource>Other</IncomeSource>
<IncomeAmount>600.00</IncomeAmount>
<PaymentPeriod>Other</PaymentPeriod>
</Income>
<Employer>
<EmployerDetails>
<Identification>
<IDValue>Alor</IDValue>
</Identification>
<CompanyName>Alor</CompanyName>
<Address AddressType="other">
<Description>Office Address</Description>
<Address>1000 place Blvd</Address>
<City>Kennesaw</City>
<State>GA</State>
<PostalCode>30318</PostalCode>
</Address>
<Phone PhoneType="office">
<PhoneDescription>Office</PhoneDescription>
<PhoneNumber>770-400-0000</PhoneNumber>
</Phone>
</EmployerDetails>
<Income>
<IncomeAmount>1600</IncomeAmount>
<PaymentPeriod>Monthly</PaymentPeriod>
</Income>
</Employer>
<Contact>
<Identification>
<IDValue>Ronnie Smith</IDValue>
</Identification>
<Name>
<FirstName>Ronnie Smith</FirstName>
<LastName />
</Name>
<Address AddressType="other">
<Description>Residents Contact Address</Description>
<Address />
<City />
<State />
<PostalCode />
</Address>
<Phone PhoneType="other">
<PhoneNumber>678-999-9999</PhoneNumber>
</Phone>
</Contact>
<CustomRecords>
<Record>
<Name>Attachment</Name>
<Value>t0143556_Application(1).pdf</Value>
</Record>
<Record>
<Name>Attachment</Name>
<Value>t0143556_Application.pdf</Value>
</Record>
<Record>
<Name>Attachment</Name>
<Value>t0143556_Qualifying-Criteria-Cb-Lofts.pdf</Value>
</Record>
</CustomRecords>
</Tenants>
<Tenants>
<PersonDetails>
<Identification>
<IDValue>r0034758</IDValue>
<OrganizationName>Roommate</OrganizationName>
</Identification>
<Name>
<FirstName>Aj</FirstName>
<LastName>Gober</LastName>
</Name>
<Address AddressType="current">
<Description>Prospect Current Address</Description>
<Address>118 Collier Rd</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30318</PostalCode>
</Address>
<Address AddressType="other">
<Email>aj@gmail.com</Email>
</Address>
<Phone PhoneType="cell">
<PhoneDescription>Mobile</PhoneDescription>
<PhoneNumber>4709999999</PhoneNumber>
</Phone>
</PersonDetails>
<DateOfBirth>1996-01-01</DateOfBirth>
<SSN>199-99-9999</SSN>
<DriversLicense>GA 057513427</DriversLicense>
<Guarantor>false</Guarantor>
<Income>
<IncomeSource />
<IncomeAmount>0.00</IncomeAmount>
<PaymentPeriod>Other</PaymentPeriod>
</Income>
<Employer>
<EmployerDetails>
<Identification>
<IDValue>Alor</IDValue>
</Identification>
<CompanyName>Alor</CompanyName>
<Address AddressType="other">
<Description>Office Address</Description>
<Address>2120 Barrett</Address>
<City>Kennesaw</City>
<State>GA</State>
<PostalCode>30144</PostalCode>
</Address>
<Phone PhoneType="office">
<PhoneDescription>Office</PhoneDescription>
<PhoneNumber>470-999-9999</PhoneNumber>
</Phone>
</EmployerDetails>
<Income>
<IncomeAmount>1600</IncomeAmount>
<PaymentPeriod>Monthly</PaymentPeriod>
</Income>
</Employer>
<Contact>
<Identification>
<IDValue>Ruth G</IDValue>
</Identification>
<Name>
<FirstName>Ruth G</FirstName>
<LastName />
</Name>
<Address AddressType="current">
<Description>Prospects Contact Address</Description>
<Address>5832 Hunt Club Run </Address>
<City>Norcross</City>
<State>GA</State>
<PostalCode>30093</PostalCode>
</Address>
<Phone PhoneType="other">
<PhoneNumber>770-999-9999</PhoneNumber>
</Phone>
</Contact>
<CustomRecords>
<Record>
<Name>DOB</Name>
<Value>01/01/1996</Value>
</Record>
<Record>
<Name>Relationship</Name>
<Value>Roommate</Value>
</Record>
</CustomRecords>
</Tenants>
<Tenants>
<PersonDetails>
<Identification>
<IDValue>r0034758</IDValue>
<OrganizationName>Roommate</OrganizationName>
</Identification>
<Name>
<FirstName>Aaij</FirstName>
<LastName>Gob</LastName>
</Name>
<Address AddressType="current">
<Description>Prospect Current Address</Description>
<Address>118 Collier Rd</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30318</PostalCode>
</Address>
<Address AddressType="other">
<Email>aair@gmail.com</Email>
</Address>
<Phone PhoneType="cell">
<PhoneDescription>Mobile</PhoneDescription>
<PhoneNumber>4709999999</PhoneNumber>
</Phone>
</PersonDetails>
<DateOfBirth>1996-01-13</DateOfBirth>
<SSN>199-99-9999</SSN>
<DriversLicense>GA 0999999</DriversLicense>
<Guarantor>false</Guarantor>
<Employer>
<EmployerDetails>
<Identification>
<IDValue>Alorica</IDValue>
</Identification>
<CompanyName>Alor</CompanyName>
<Address AddressType="other">
<Description>Office Address</Description>
<Address>2120 Barrett</Address>
<City>Kennesaw</City>
<State>GA</State>
<PostalCode>30144</PostalCode>
</Address>
<Phone PhoneType="office">
<PhoneDescription>Office</PhoneDescription>
<PhoneNumber>470-999-9999</PhoneNumber>
</Phone>
</EmployerDetails>
<Income>
<IncomeAmount>1600</IncomeAmount>
<PaymentPeriod>Monthly</PaymentPeriod>
</Income>
</Employer>
<Contact>
<Identification>
<IDValue>Ruth G</IDValue>
</Identification>
<Name>
<FirstName>Ruth G</FirstName>
<LastName />
</Name>
<Address AddressType="current">
<Description>Prospects Contact Address</Description>
<Address>5832 Hunt Club Run</Address>
<City>Norcross</City>
<State>GA</State>
<PostalCode>30093</PostalCode>
</Address>
<Phone PhoneType="other">
<PhoneNumber>770-999-9999</PhoneNumber>
</Phone>
</Contact>
<CustomRecords>
<Record>
<Name>DOB</Name>
<Value>01/01/1996</Value>
</Record>
<Record>
<Name>Relationship</Name>
<Value>Roommate</Value>
</Record>
</CustomRecords>
</Tenants>
<FileTransactions>
<Identification>
<IDValue>702571946</IDValue>
</Identification>
<TransDate>2017-04-28</TransDate>
<TransTypeDesc>Administrative Income</TransTypeDesc>
<TransType>Charge</TransType>
<TransAmount>75.00</TransAmount>
<OpenAmount>75.00</OpenAmount>
<CustomRecords>
<Record>
<Name>Charge Code</Name>
<Value>appfee</Value>
</Record>
<Record>
<Name>Charge Code Desc</Name>
<Value>Application Fee</Value>
</Record>
<Record>
<Name>Transaction Note</Name>
<Value>Application Fee :Write Off by Charge Ctrl# 3053299</Value>
</Record>
</CustomRecords>
</FileTransactions>
<FileTransactions>
<Identification>
<IDValue>702571947</IDValue>
</Identification>
<TransDate>2017-04-28</TransDate>
<TransTypeDesc>Administrative Income</TransTypeDesc>
<TransType>Charge</TransType>
<TransAmount>200.00</TransAmount>
<OpenAmount>200.00</OpenAmount>
<CustomRecords>
<Record>
<Name>Charge Code</Name>
<Value>admin</Value>
</Record>
<Record>
<Name>Charge Code Desc</Name>
<Value>Administration Fee</Value>
</Record>
<Record>
<Name>Transaction Note</Name>
<Value>Administration Fee :Write Off by Charge Ctrl# 2892820</Value>
</Record>
</CustomRecords>
</FileTransactions>
</LeaseFile>
<LeaseFile>
<Identification>
<IDValue>t0285552</IDValue>
<OrganizationName>Primary Tenant Code</OrganizationName>
</Identification>
<Identification>
<IDValue>214</IDValue>
<OrganizationName>Unit Code</OrganizationName>
</Identification>
<CollectionStatus>true</CollectionStatus>
<CollectionStatusLastChangeDate>2017-11-03</CollectionStatusLastChangeDate>
<LeaseBegin>2017-07-24</LeaseBegin>
<LeaseEnd>2018-07-23</LeaseEnd>
<MonthlyRentAmount>1181.00</MonthlyRentAmount>
<Tenants>
<PersonDetails>
<Identification>
<IDValue>t0285552</IDValue>
</Identification>
<Name>
<FirstName>Ahshion</FirstName>
<LastName>Nea</LastName>
</Name>
<Address AddressType="current">
<Description>current</Description>
<Address>1000 Bar</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30324</PostalCode>
</Address>
<Address AddressType="current">
<Description>current</Description>
<Address>2430 Cheshire Bridge Road</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30324</PostalCode>
</Address>
<Address AddressType="previous">
<Description>previous</Description>
<Address>1000 Bar</Address>
<City>Atlanta</City>
<State>GA</State>
<PostalCode>30324</PostalCode>
</Address>
<Address AddressType="other">
<Email>ral6@gmail.com</Email>
</Address>
<Phone PhoneType="home">
<PhoneDescription>Home</PhoneDescription>
<PhoneNumber>7169999999</PhoneNumber>
</Phone>
</PersonDetails>
<DateOfBirth>1996-01-01</DateOfBirth>
<SSN>199-99-9999</SSN>
<DriversLicense>NY 403</DriversLicense>
<Income>
<IncomeSource />
<IncomeAmount>0.00</IncomeAmount>
<PaymentPeriod>Other</PaymentPeriod>
</Income>
<Employer>
<EmployerDetails>
<Identification>
<IDValue>Comcast</IDValue>
</Identification>
<CompanyName>Comcast</CompanyName>
<Address AddressType="other">
<Description>Office Address</Description>
<Address>4700 N Point</Address>
<City>Alpharetta</City>
<State>GA</State>
<PostalCode>30005</PostalCode>
</Address>
<Phone PhoneType="office">
<PhoneDescription>Office</PhoneDescription>
<PhoneNumber>(678) 999-9999</PhoneNumber>
</Phone>
</EmployerDetails>
<Income>
<IncomeAmount>0</IncomeAmount>
<PaymentPeriod>Monthly</PaymentPeriod>
</Income>
</Employer>
<Contact>
<Identification>
<IDValue>Kiron McC</IDValue>
<OrganizationName>Friend</OrganizationName>
</Identification>
<Name>
<FirstName>Kiron McC</FirstName>
<LastName />
</Name>
<Address AddressType="other">
<Description>Residents Contact Address</Description>
<Address />
<City />
<State />
<PostalCode />
</Address>
<Phone PhoneType="other">
<PhoneNumber>4049999999</PhoneNumber>
</Phone>
</Contact>
</Tenants>
<FileTransactions>
<Identification>
<IDValue>702888725</IDValue>
</Identification>
<TransDate>2017-07-21</TransDate>
<TransTypeDesc>Administrative Income</TransTypeDesc>
<TransType>Charge</TransType>
<TransAmount>200.00</TransAmount>
<OpenAmount>200.00</OpenAmount>
<CustomRecords>
<Record>
<Name>Charge Code</Name>
<Value>admin</Value>
</Record>
<Record>
<Name>Charge Code Desc</Name>
<Value>Administration Fee</Value>
</Record>
<Record>
<Name>Transaction Note</Name>
<Value>Administrative Fee (Ahshion Neasman) :Write Off by Charge Ctrl# 3211113</Value>
</Record>
</CustomRecords>
</FileTransactions>
<FileTransactions>
<Identification>
<IDValue>702888726</IDValue>
</Identification>
<TransDate>2017-07-21</TransDate>
<TransTypeDesc>Administrative Income</TransTypeDesc>
<TransType>Charge</TransType>
<TransAmount>75.00</TransAmount>
<OpenAmount>75.00</OpenAmount>
<CustomRecords>
<Record>
<Name>Charge Code</Name>
<Value>appfee</Value>
</Record>
<Record>
<Name>Charge Code Desc</Name>
<Value>Application Fee</Value>
</Record>
<Record>
<Name>Transaction Note</Name>
<Value>Application Fee (Ahshion Neasman) :Write Off by Charge Ctrl# 3211101</Value>
</Record>
</CustomRecords>
</FileTransactions>
<FileTransactions>
<Identification>
<IDValue>703092265</IDValue>
</Identification>
<TransDate>2017-08-25</TransDate>
<TransTypeDesc>Late / NSF Fees</TransTypeDesc>
<TransType>Charge</TransType>
<TransAmount>50.00</TransAmount>
<OpenAmount>50.00</OpenAmount>
<CustomRecords>
<Record>
<Name>Charge Code</Name>
<Value>nsf</Value>
</Record>
<Record>
<Name>Charge Code Desc</Name>
<Value>NSF Fee</Value>
</Record>
<Record>
<Name>Transaction Note</Name>
<Value>Returned check charge :Write Off by Charge Ctrl# 3211110</Value>
</Record>
</CustomRecords>
</FileTransactions>
</LeaseFile>
</LeaseFiles>
</PropertyFile>
希望生成的文件是管道分隔的并且包含所有字段,无论它们是否有数据....我也有模式,所以不确定我是否可以将它与那些以及任何不是的节点进行比较在&#39;记录&#39;它填充空白...如果分隔文件中的值是nodeName:text值,我也可以得到它,然后我可以从那里解析它....
答案 0 :(得分:0)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" />
<!-- define an array containing the fields we are interested in -->
<xsl:variable name="fieldArray">
<field>TotalProperties</field>
<field>TotalLeaseFiles</field>
<field>TotalTenants</field>
<field>TotalOpenAmount</field>
<field>CollectionStatus</field>
<field>MoveInDate</field>
<field>MoveOutDate</field>
<field>TenantFirstName</field>
<field>TenantLastName</field>
<field>TenantCity</field>
<field>ContactFirstName</field>
<field>ContactLastName</field>
<!-- Add the rest of the fields. -->
</xsl:variable>
<xsl:variable name="delimiter" select="'|'" />
<xsl:variable name="fields" select="msxml:node-set($fieldArray)"/>
<xsl:template match="/">
<!-- output the header row -->
<xsl:for-each select="$fields/field">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<!-- Store the summary values in a variable. -->
<xsl:variable name="summary">
<xsl:apply-templates select=".//Summary//TotalProperties" mode="values">
<xsl:with-param name="name" select="'TotalProperties'"/>
</xsl:apply-templates>
<xsl:apply-templates select=".//Summary//TotalLeaseFiles" mode="values">
<xsl:with-param name="name" select="'TotalLeaseFiles'"/>
</xsl:apply-templates>
<xsl:apply-templates select=".//Summary//TotalTenants" mode="values">
<xsl:with-param name="name" select="'TotalTenants'"/>
</xsl:apply-templates>
<xsl:apply-templates select=".//Summary//TotalOpenAmount" mode="values">
<xsl:with-param name="name" select="'TotalOpenAmount'"/>
</xsl:apply-templates>
<!-- Add any more values from the summary field here. -->
</xsl:variable>
<xsl:variable name="summaryList" select="msxml:node-set($summary)"/>
<xsl:apply-templates select=".//LeaseFile">
<xsl:with-param name="summaryList" select="$summaryList"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="LeaseFile">
<xsl:param name="summaryList"/>
<!-- output newline -->
<xsl:text>
</xsl:text>
<!-- Store the lease file values in a variable. -->
<xsl:variable name="leaseFileValues">
<xsl:apply-templates select=".//MoveInDate" mode="values">
<xsl:with-param name="name" select="'MoveInDate'"/>
</xsl:apply-templates>
<xsl:apply-templates select=".//MoveOutDate" mode="values">
<xsl:with-param name="name" select="'MoveOutDate'"/>
</xsl:apply-templates>
<xsl:apply-templates select=".//CollectionStatus" mode="values">
<xsl:with-param name="name" select="'CollectionStatus'"/>
</xsl:apply-templates>
<xsl:apply-templates select="Tenants/PersonDetails/Name/FirstName" mode="values">
<xsl:with-param name="name" select="'TenantFirstName'"/>
</xsl:apply-templates>
<xsl:apply-templates select="Tenants/PersonDetails/Address/City" mode="values">
<xsl:with-param name="name" select="'TenantCity'"/>
</xsl:apply-templates>
<xsl:apply-templates select="Tenants/PersonDetails/Name/LastName" mode="values">
<xsl:with-param name="name" select="'TenantLastName'"/>
</xsl:apply-templates>
<xsl:apply-templates select="Tenants/Contact/Name/FirstName" mode="values">
<xsl:with-param name="name" select="'ContactFirstName'"/>
</xsl:apply-templates>
<xsl:apply-templates select="Tenants/Contact/Name/LastName" mode="values">
<xsl:with-param name="name" select="'ContactLastName'"/>
</xsl:apply-templates>
<!-- Add the rest of the fields. -->
</xsl:variable>
<xsl:variable name="leaseFileValuesList" select="msxml:node-set($leaseFileValues)"/>
<!-- Loop through the field. -->
<xsl:for-each select="$fields/field">
<xsl:variable name="currentField" select="."/>
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:for-each select="$summaryList/element[name=$currentField and value!='' and not(value = preceding-sibling::element[name=$currentField][1]/value)]/value">
<xsl:if test="position() != 1">
<xsl:value-of select="'^'"/>
</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select="$leaseFileValuesList/element[name=$currentField and value!='' and not(value = preceding-sibling::element[name=$currentField][1]/value)]/value">
<xsl:if test="position() != 1">
<xsl:value-of select="'^'"/>
</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="*" mode="values">
<xsl:param name="name"/>
<element>
<name>
<xsl:value-of select="$name"/>
</name>
<value>
<xsl:value-of select="."/>
</value>
</element>
</xsl:template>
</xsl:stylesheet>