我继承了一个相当难看的xsl脚本,并且对xslt的使用经验非常有限。我昨天得到了很多帮助,但仍然有点挣扎,所以我提供了整个xml和更好的想要输出的描述。
我认为通过将整个脚本包装在模板中并选择是一件非常麻烦的事情,因为当我进行建议的更改时我不断收到编译错误,而且因为我有点失明,所以我很清楚我缺乏足够的理解和获得它的时间,所以我真诚地感谢一些帮助。
在这个阶段,我只关心组件4 。一旦我坚持下去,我相信我将能够重写其余的剧本。
我得到的输出;
contactID,mediumCode,areaCode,communicationDetails
"0123456789","T","02","62881111"
"5290001890","T","02","92881781"
"4400139361","T","07","49281771"
"6600027368","T","07","48103280"
我需要的输出;
contactID,mediumCode,areaCode,communicationDetails
"0123456789","T","02","62881111"
"5290001890","T","02","92881781"
"4400139361","T","07","49281771"
"6600027368","T","07","48103280"
"0123456789","E","","john.smith@myisp.com.au"
"5290001890","E","","rabina.smiley@ekit.com.au"
"4400139361","E","","suzanne.jones2@optus.com"
"6600027368","E","","maryann.smart@dodo.com"
"0123456789","M","","04140012225"
"5290001890","M","","04290012333"
"4400139361","M","","0404009266"
"6600027368","M","","0414003242"
XSL样式表:
<!-- AHPRA XML CSV Converter Script -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:strip-space elements="*" />
<!-- BATCH FILE COMPONENT -->
<xsl:param name="component"/>
<!-- 0=requestHeader, 1=personalDetails, 2=residencyDetails, 3=addressDetails, 4=communications, 5=primaryQualification, 6=additionalQualifications, 7=legacyDetails, 8=registrationDetails, 9=specialtyDetails, 10=conditionDetails, 11=endorsementDetails, 12=undertakingDetails, 13=notationDetails, 14=employmentDetails -->
<!-- FORMAT DATE -->
<xsl:template name="formatDate">
<xsl:param name="date"/>
<xsl:choose>
<xsl:when test="boolean($date)">
<xsl:if test="string-length($date)>0">
<xsl:value-of select="concat(substring($date,9,2),'/',substring($date,6,2),'/',substring($date,1,4))" />
</xsl:if>
</xsl:when>
<xsl:otherwise> </xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- NL2BR -->
<xsl:template name="nl2br">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,' ')">
<xsl:value-of select="substring-before($string, ' ')" disable-output-escaping="yes"/>
<br/>
<xsl:call-template name="nl2br"><xsl:with-param name="string" select="substring-after($string,' ')"/></xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:choose>
<!-- COMPONENT 0 requestHeader -->
<!-- runType, dateStamp, sequenceNumber -->
<xsl:when test="$component=0">
<xsl:for-each select="medicare">
"<xsl:value-of select="requestHeader/runType" />",
<xsl:call-template name="formatDate">
<xsl:with-param name="date" select="requestHeader/dateStamp"/>
</xsl:call-template>,
"<xsl:value-of select="requestHeader/sequenceNumber" />
"<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:when>
<!-- COMPONENT 1 personalDetails -->
<!-- contactID, givenName, middleName, familyName, title, gender, dateOfBirth, dobAccuracy, dateOfDeath -->
<xsl:when test="$component=1">
<xsl:for-each select="//person">
<xsl:if test="string-length(concat(personalDetails/givenName,personalDetails/middleName,personalDetails/familyName,personalDetails/title,personalDetails/gender,personalDetails/dateOfBirth,personalDetails/dobAccuracy,personalDetails/dateOfDeath))!=0">
"<xsl:value-of select="contactID" />",
"<xsl:value-of select="personalDetails/givenName" />",
"<xsl:value-of select="personalDetails/middleName" />",
"<xsl:value-of select="personalDetails/familyName" />",
"<xsl:value-of select="personalDetails/title" />",
"<xsl:value-of select="personalDetails/gender" />",
<xsl:call-template name="formatDate"><xsl:with-param name="date" select="personalDetails/dateOfBirth"/></xsl:call-template>,
"<xsl:value-of select="personalDetails/dobAccuracy" />",
<xsl:call-template name="formatDate"><xsl:with-param name="date" select="personalDetails/dateOfDeath"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<!-- COMPONENT 4 communications -->
<!-- contactID, mediumCode, areaCode, communicationDetails -->
<xsl:variable name="q" select="'"'" />
<xsl:variable name="c" select="', '" />
<xsl:when test="$component=4">
<xsl:for-each select="//person">
<xsl:if test="string-length(concat(communications/communication/mediumCode,communications/communication/areaCode,communications/communication/communicationDetails))!=0">
<xsl:value-of select="concat($q, contactID, $q, $c, $q,
normalize-space(communications/communication/mediumCode), $q, $c, $q,
normalize-space(communications/communication/areaCode), $q, $c, $q,
normalize-space(communications/communication/communicationDetails), $q)" />
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XML数据:
<medicare>
<requestHeader>
<runType>O</runType>
<dateStamp>2011-05-30</dateStamp>
<sequenceNumber>10</sequenceNumber>
</requestHeader>
<person>
<contactID>0123456789</contactID>
<personalDetails>
<givenName>John</givenName>
<middleName>Alan</middleName>
<familyName>Smith</familyName>
<title>DR</title>
<gender>M</gender>
<dateOfBirth>1932-03-10</dateOfBirth>
<dobAccuracy>AAA</dobAccuracy>
<dateOfDeath>1932-03-11</dateOfDeath>
</personalDetails>
<residencyDetails>
<residencyStatus>Permanent Resident</residencyStatus>
<visaType>Temporary</visaType>
<passportNum>F1925190</passportNum>
<countryOfIssue>Australia</countryOfIssue>
</residencyDetails>
<addressDetails>
<address>
<addressPurposeIndicator>Preferred Address</addressPurposeIndicator>
<floorLevelNum>10</floorLevelNum>
<floorLevelType>Ground</floorLevelType>
<lotNum>72</lotNum>
<postcode>2000</postcode>
<state>NSW</state>
<streetName>Main</streetName>
<streetNum>112</streetNum>
<streetType>RD</streetType>
<locality>SYDNEY</locality>
<flatUnitNum>3</flatUnitNum>
<flatUnitType>Unit</flatUnitType>
<sitePremisesName>St Vincents</sitePremisesName>
</address>
<address>
<addressPurposeIndicator>Principle Place of Practice</addressPurposeIndicator>
<floorLevelNum />
<floorLevelType />
<lotNum />
<postcode>2000</postcode>
<state>NSW</state>
<streetName>Station</streetName>
<streetNum />
<streetType>ST</streetType>
<locality>SYDNEY</locality>
<flatUnitNum />
<flatUnitType />
<sitePremisesName>North Shore Hospital</sitePremisesName>
</address>
</addressDetails>
<communications>
<communication>
<mediumCode>T</mediumCode>
<areaCode>02</areaCode>
<communicationDetails>62881111</communicationDetails>
</communication>
<communication>
<mediumCode>E</mediumCode>
<communicationDetails>john.smith@myisp.com.au</communicationDetails>
</communication>
<communication>
<mediumCode>M</mediumCode>
<communicationDetails>04140012225</communicationDetails>
</communication>
</communications>
<registrationDetails>
<registration>
<registrationID>MED0000000009</registrationID>
<profession>Medical Practitioner</profession>
<division />
<initialRegistrationStartDate>1977-08-03</initialRegistrationStartDate>
<registrationType>General</registrationType>
<registrationSubType />
<registrationTypeStartDate>2011-04-30</registrationTypeStartDate>
<registrationStatus>Registered</registrationStatus>
<registrationStatusStartDate>2011-05-30</registrationStatusStartDate>
<specialtyDetails>
<specialtyDetail>
<specialty>Heart Bypass</specialty>
<fieldOfSpecialtyPractice>Surgery</fieldOfSpecialtyPractice>
<specialtyPracticeStartDate>2010-02-20</specialtyPracticeStartDate>
<specialtyPracticeEndDate>2011-04-23</specialtyPracticeEndDate>
</specialtyDetail>
</specialtyDetails>
<conditionDetails>
<condition>
<applicationRegistrationCondition>APPROVED</applicationRegistrationCondition>
<conditionApprovedDate>1805-12-08</conditionApprovedDate>
<conditionDetail>The quick brown fox jumped over the lazy dog.</conditionDetail>
</condition>
</conditionDetails>
<endorsementDetails>
<endorsement>
<endorsementType>Medicine Man</endorsementType>
<endorsementSubType>Voodoo</endorsementSubType>
<endorsementCreateDate>2002-08-20</endorsementCreateDate>
<endorsementEndDate>2017-06-06</endorsementEndDate>
</endorsement>
</endorsementDetails>
<undertakingDetails>
<undertaking>
<undertakingType>Further Study</undertakingType>
<undertakingApprovedDate>2001-07-11</undertakingApprovedDate>
<undertakingText>The Doctor will undertake further study.</undertakingText>
</undertaking>
</undertakingDetails>
<notationDetails>
<notationDetail>
<notationCreateDate />
<notationEndDate />
<notation />
</notationDetail>
</notationDetails>
</registration>
<registration>
<registrationID>MED0000000010</registrationID>
<profession>Medical Practitioner</profession>
<division />
<initialRegistrationStartDate>1977-08-03</initialRegistrationStartDate>
<registrationType>Specialist</registrationType>
<registrationSubType />
<registrationTypeStartDate>2011-04-30</registrationTypeStartDate>
<registrationStatus>Registered</registrationStatus>
<registrationStatusStartDate>2011-05-30</registrationStatusStartDate>
<specialtyDetails>
<specialtyDetail>
<specialty>Pathology</specialty>
<fieldOfSpecialtyPractice>General pathology</fieldOfSpecialtyPractice>
<specialtyPracticeStartDate>2010-11-01</specialtyPracticeStartDate>
<specialtyPracticeEndDate />
</specialtyDetail>
</specialtyDetails>
<conditionDetails>
<condition>
<applicationRegistrationCondition>APPROVED</applicationRegistrationCondition>
<conditionApprovedDate>1805-12-08</conditionApprovedDate>
<conditionDetail>The quick brown fox jumped over the lazy dog.</conditionDetail>
</condition>
</conditionDetails>
<endorsementDetails>
<endorsement>
<endorsementType />
<endorsementSubType />
<endorsementCreateDate />
<endorsementEndDate />
</endorsement>
</endorsementDetails>
<undertakingDetails>
<undertaking>
<undertakingType></undertakingType>
<undertakingApprovedDate></undertakingApprovedDate>
<undertakingText></undertakingText>
</undertaking>
</undertakingDetails>
<notationDetails>
<notationDetail>
<notationCreateDate />
<notationEndDate />
<notation />
</notationDetail>
</notationDetails>
</registration>
</registrationDetails>
<employmentDetails>
<positionTitle>Chief Medical Officer</positionTitle>
<sponsorOrganisationName>North Shore Hospital</sponsorOrganisationName>
<sponsorOrganisationContact>Peter Piper</sponsorOrganisationContact>
</employmentDetails>
</person>
<person>
<contactID>5290001890</contactID>
<personalDetails>
<givenName>Rabina</givenName>
<middleName>Dora</middleName>
<familyName>Smiley</familyName>
<title>DR</title>
<gender>F</gender>
<dateOfBirth>1961-03-22</dateOfBirth>
<dobAccuracy>AAA</dobAccuracy>
<dateOfDeath>1900-12-01</dateOfDeath>
</personalDetails>
<residencyDetails>
<residencyStatus>Australian Citizen</residencyStatus>
<visaType>Perm</visaType>
<passportNum>P1234567</passportNum>
<countryOfIssue>New Zealand</countryOfIssue>
</residencyDetails>
<addressDetails>
<address>
<addressPurposeIndicator>Both preferred and practice</addressPurposeIndicator>
<floorLevelNum />
<floorLevelType />
<lotNum />
<postcode>2040</postcode>
<state>NSW</state>
<streetName>Plenty</streetName>
<streetNum>29</streetNum>
<streetType>RD</streetType>
<locality>LEICHHARDT</locality>
<flatUnitNum />
<flatUnitType />
<sitePremisesName />
</address>
</addressDetails>
<communications>
<communication>
<mediumCode>T</mediumCode>
<areaCode>02</areaCode>
<communicationDetails>92881781</communicationDetails>
</communication>
<communication>
<mediumCode>E</mediumCode>
<communicationDetails>rabina.smiley@ekit.com</communicationDetails>
</communication>
<communication>
<mediumCode>M</mediumCode>
<communicationDetails>04290012333</communicationDetails>
</communication>
</communications>
<registrationDetails>
<registration>
<registrationID>MED0000000219</registrationID>
<profession>Medical Practitioner</profession>
<division />
<initialRegistrationStartDate>2010-04-15</initialRegistrationStartDate>
<registrationType>Limited</registrationType>
<registrationSubType>Area of Need</registrationSubType>
<registrationTypeStartDate>2010-05-20</registrationTypeStartDate>
<registrationStatus>Surrendered</registrationStatus>
<registrationStatusStartDate>2011-05-29</registrationStatusStartDate>
<specialtyDetails>
<specialtyDetail>
<specialty>Delivering Babies</specialty>
<fieldOfSpecialtyPractice>Midwifery</fieldOfSpecialtyPractice>
<specialtyPracticeStartDate>2009-01-31</specialtyPracticeStartDate>
<specialtyPracticeEndDate>2099-04-08</specialtyPracticeEndDate>
</specialtyDetail>
</specialtyDetails>
<conditionDetails>
<condition>
<applicationRegistrationCondition>Area of Need</applicationRegistrationCondition>
<conditionApprovedDate>2010-05-28</conditionApprovedDate>
<conditionDetail>PRE AMC TRAINING - TO WORK WITHIN EMERGENCY AT LYELL MCEWIN HOSPITAL - UNDER SUPERVISION OF DR H BLAH, DR B BLOGS NOMINATED SUPERVISORS.</conditionDetail>
</condition>
<condition>
<applicationRegistrationCondition>Area of Need</applicationRegistrationCondition>
<conditionApprovedDate>2011-05-22</conditionApprovedDate>
<conditionDetail>DR B BLOGS NOMINATED SUPERVISOR</conditionDetail>
</condition>
</conditionDetails>
<endorsementDetails>
<endorsement>
<endorsementType />
<endorsementSubType />
<endorsementCreateDate />
<endorsementEndDate />
</endorsement>
</endorsementDetails>
<undertakingDetails>
<undertaking>
<undertakingType>University Lectures</undertakingType>
<undertakingApprovedDate>2014-12-18</undertakingApprovedDate>
<undertakingText>The Doctor will undertake to deliver lectures to uni students.</undertakingText>
</undertaking>
</undertakingDetails>
<notationDetails>
<notationDetail>
<notationCreateDate>1980-09-11</notationCreateDate>
<notationEndDate>2016-09-11</notationEndDate>
<notation>Ineligible Orderly</notation>
</notationDetail>
</notationDetails>
</registration>
</registrationDetails>
<employmentDetails>
<positionTitle>Dr Dre</positionTitle>
<sponsorOrganisationName>Aftermath Hospital</sponsorOrganisationName>
<sponsorOrganisationContact>Eminem</sponsorOrganisationContact>
</employmentDetails>
</person>
<person>
<contactID>4400139361</contactID>
<personalDetails>
<givenName>Suzanne</givenName>
<middleName>Lillian</middleName>
<familyName>Jones</familyName>
<title>MS</title>
<gender>F</gender>
<dateOfBirth>1971-12-13</dateOfBirth>
<dobAccuracy>AAA</dobAccuracy>
<dateOfDeath>1905-09-22</dateOfDeath>
</personalDetails>
<residencyDetails>
<residencyStatus>Australian Citizen</residencyStatus>
<visaType />
<passportNum />
<countryOfIssue />
</residencyDetails>
<addressDetails>
<address>
<addressPurposeIndicator>Both preferred and practice</addressPurposeIndicator>
<floorLevelNum />
<floorLevelType />
<lotNum />
<postcode>4740</postcode>
<state>QLD</state>
<streetName>The Avenue</streetName>
<streetNum>419</streetNum>
<streetType>ST</streetType>
<locality>ANDERGROVE</locality>
<flatUnitNum />
<flatUnitType />
<sitePremisesName />
</address>
</addressDetails>
<communications>
<communication>
<mediumCode>T</mediumCode>
<areaCode>07</areaCode>
<communicationDetails>49281771</communicationDetails>
</communication>
<communication>
<mediumCode>E</mediumCode>
<communicationDetails>suzanne.jones2@optus.com</communicationDetails>
</communication>
<communication>
<mediumCode>M</mediumCode>
<communicationDetails>0404009266</communicationDetails>
</communication>
</communications>
<registrationDetails>
<registration>
<registrationID>NMW0000003085</registrationID>
<profession>Nurse</profession>
<division>Registered Nurse (Division 1)</division>
<initialRegistrationStartDate>1994-03-15</initialRegistrationStartDate>
<registrationType>General</registrationType>
<registrationSubType />
<registrationTypeStartDate>1994-03-15</registrationTypeStartDate>
<registrationStatus>Registered</registrationStatus>
<registrationStatusStartDate>2011-05-27</registrationStatusStartDate>
<specialtyDetails>
<specialtyDetail>
<specialty>Being Excellent</specialty>
<fieldOfSpecialtyPractice>Excellence</fieldOfSpecialtyPractice>
<specialtyPracticeStartDate>2008-05-29</specialtyPracticeStartDate>
<specialtyPracticeEndDate>2018-11-15</specialtyPracticeEndDate>
</specialtyDetail>
</specialtyDetails>
<conditionDetails>
<condition>
<applicationRegistrationCondition>REJECTED</applicationRegistrationCondition>
<conditionApprovedDate>1805-04-08</conditionApprovedDate>
<conditionDetail>The lazy dog jumped up to bite the fox.</conditionDetail>
</condition>
</conditionDetails>
<endorsementDetails>
<endorsement>
<endorsementType>Nurse Practitioner</endorsementType>
<endorsementSubType>Nurse</endorsementSubType>
<endorsementCreateDate>2011-04-30</endorsementCreateDate>
<endorsementEndDate />
</endorsement>
</endorsementDetails>
<undertakingDetails>
<undertaking>
<undertakingType>Education</undertakingType>
<undertakingApprovedDate>2011-05-01</undertakingApprovedDate>
<undertakingText>The Doctor will undertake training in new Pathology methods.</undertakingText>
</undertaking>
</undertakingDetails>
<notationDetails>
<notationDetail>
<notationCreateDate />
<notationEndDate />
<notation />
</notationDetail>
</notationDetails>
</registration>
</registrationDetails>
<employmentDetails>
<positionTitle />
<sponsorOrganisationName />
<sponsorOrganisationContact />
</employmentDetails>
</person>
<person>
<contactID>6600027368</contactID>
<personalDetails>
<givenName>Mary</givenName>
<middleName>Ann</middleName>
<familyName>Smart</familyName>
<title>MRS</title>
<gender>F</gender>
<dateOfBirth>1970-10-03</dateOfBirth>
<dobAccuracy>AAA</dobAccuracy>
<dateOfDeath />
</personalDetails>
<residencyDetails>
<residencyStatus>Kiwi</residencyStatus>
<visaType>Temp</visaType>
<passportNum>K7777777</passportNum>
<countryOfIssue>New Zealand</countryOfIssue>
</residencyDetails>
<addressDetails>
<address>
<addressPurposeIndicator>Both preferred and practice</addressPurposeIndicator>
<floorLevelNum />
<floorLevelType />
<lotNum />
<postcode>4214</postcode>
<state>QLD</state>
<streetName>The Terrace</streetName>
<streetNum>39</streetNum>
<streetType>RD</streetType>
<locality>ASHMORE</locality>
<flatUnitNum />
<flatUnitType />
<sitePremisesName />
</address>
</addressDetails>
<communications>
<communication>
<mediumCode>T</mediumCode>
<areaCode>07</areaCode>
<communicationDetails>48103280</communicationDetails>
</communication>
<communication>
<mediumCode>E</mediumCode>
<communicationDetails>maryann.smart@dodo.com</communicationDetails>
</communication>
<communication>
<mediumCode>M</mediumCode>
<communicationDetails>0414003242</communicationDetails>
</communication>
</communications>
<registrationDetails>
<registration>
<registrationID>NMW0000114235</registrationID>
<profession>Midwife</profession>
<division />
<initialRegistrationStartDate>1994-02-10</initialRegistrationStartDate>
<registrationType>General</registrationType>
<registrationSubType />
<registrationTypeStartDate>1994-02-10</registrationTypeStartDate>
<registrationStatus>Registered</registrationStatus>
<registrationStatusStartDate>2011-05-21</registrationStatusStartDate>
<specialtyDetails>
<specialtyDetail>
<specialty />
<fieldOfSpecialtyPractice />
<specialtyPracticeStartDate />
<specialtyPracticeEndDate />
</specialtyDetail>
</specialtyDetails>
<conditionDetails>
<condition>
<applicationRegistrationCondition />
<conditionApprovedDate />
<conditionDetail />
</condition>
</conditionDetails>
<endorsementDetails>
<endorsement>
<endorsementType>Doctor</endorsementType>
<endorsementSubType>Quack</endorsementSubType>
<endorsementCreateDate>1999-01-01</endorsementCreateDate>
<endorsementEndDate>2057-06-06</endorsementEndDate>
</endorsement>
</endorsementDetails>
<undertakingDetails>
<undertaking>
<undertakingType>Community Work</undertakingType>
<undertakingApprovedDate>2002-02-22</undertakingApprovedDate>
<undertakingText>The Doctor will undertake community work.</undertakingText>
</undertaking>
</undertakingDetails>
<notationDetails>
<notationDetail>
<notationCreateDate>2011-02-10</notationCreateDate>
<notationEndDate />
<notation>Eligible midwife</notation>
</notationDetail>
</notationDetails>
</registration>
</registrationDetails>
<employmentDetails>
<positionTitle>Doctor Feelgood</positionTitle>
<sponsorOrganisationName>Motley Crue Morgue</sponsorOrganisationName>
<sponsorOrganisationContact>Nikki Sixx</sponsorOrganisationContact>
</employmentDetails>
</person>
</medicare>
答案 0 :(得分:2)
试试这个(假设您希望每个mediumCode在一条单独的行上):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:param name="component"/>
<xsl:variable name="q" select="'"'" />
<xsl:variable name="c" select="', '" />
<xsl:template match="communication">
<xsl:choose>
<xsl:when test="$component=4">
<xsl:value-of select="concat($q,../../contactID,$q)" />
<xsl:apply-templates />
<xsl:text>
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="mediumCode | areaCode | communicationDetails">
<xsl:value-of select="concat($c,$q,.,$q)" />
</xsl:template>
<xsl:template match="contactID" />
</xsl:stylesheet>
我为逗号和引号创建了全局变量,只是为了让它在concat
函数调用中更容易阅读。
此样式表通过吐出communication
它的concatID
祖先来处理任何person
元素,然后只应用模板,这将处理它的三个子元素。
下一个模板以相同的方式处理三个子元素,输出逗号后跟引号中的值。
最后一个模板阻止contactID
自行输出。默认情况下,XSLT处理器将按顺序为XML树中的每个节点查找模板。如果没有此模板,它将采用其中文本节点的默认行为,这只是按原样输出。
如果您希望所有通信节点都在一行上,您可以这样做:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:param name="component"/>
<xsl:variable name="q" select="'"'" />
<xsl:variable name="c" select="', '" />
<xsl:template match="person">
<xsl:choose>
<xsl:when test="$component=4">
<xsl:apply-templates />
<xsl:text> </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="contactID">
<xsl:value-of select="concat($q,.,$q)" />
</xsl:template>
<xsl:template match="mediumCode | areaCode | communicationDetails">
<xsl:value-of select="concat($c,$q,.,$q)" />
</xsl:template>
</xsl:stylesheet>
编辑:这是一个解决方案,在输出的每一行中产生一致数量的字段:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:param name="component"/>
<xsl:variable name="q" select="'"'" />
<xsl:variable name="c" select="', '" />
<xsl:template match="communication[string-length(concat(mediumCode,areaCode,communicationDetails))!=0]">
<xsl:choose>
<xsl:when test="$component=4">
<xsl:value-of select="concat(
$q,../../contactID,$q,$c,
$q,mediumCode,$q,$c,
$q,areaCode,$q,$c,
$q,communicationDetails,$q,'
'
)" />
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="contactID" />
</xsl:stylesheet>
答案 1 :(得分:1)
我已经改变了你的代码以避免迭代。您只需硬编码 communication
元素的索引即可获取其他数据。我认为这在你的情况下是可以接受的。而且,我不知道你为什么使用normalize-space
函数。如果输入文档中没有多个空格,则可以避免这种情况。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:strip-space elements="*"/>
<xsl:param name="component" select="4"/>
<xsl:template match="medicare">
<xsl:choose>
<xsl:when test="$component=4">
<xsl:apply-templates select="person"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="concat(
'"', contactID, '"', ', ', '"',
normalize-space(communications/
communication[1]/mediumCode), '"', ', ', '"',
normalize-space(communications/
communication[1]/areaCode), '"', ', ', '"',
normalize-space(communications/
communication[1]/communicationDetails), '"', ', ', '"',
normalize-space(communications/
communication[2]/mediumCode), '"', ', ', '"',
normalize-space(communications/
communication[2]/communicationDetails), '"', ', ', '"',
normalize-space(communications/
communication[3]/mediumCode), '"', ', ', '"',
normalize-space(communications/
communication[3]/communicationDetails),'"')" />
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
结果是:
"0123456789", "T", "02", "62881111", "E", "john.smith@myisp.com.au", "M", "04140012225"
"5290001890", "T", "02", "92881781", "E", "rabina.smiley@ekit.com", "M", "04290012333"
下面是一种更简洁的方法,可以产生相同类型的输出(适用于您的用例非常接近您在问题中显示的数据的情况):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" media-type="text" encoding="UTF-8" indent="no" />
<xsl:strip-space elements="*"/>
<xsl:param name="component" select="4"/>
<xsl:template match="medicare">
<xsl:choose>
<xsl:when test="$component=4">
<xsl:apply-templates select="person"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="concat(
'"', contactID, '"', ', ', '"')"/>
<xsl:apply-templates select="communications/communication/*"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="communication/*">
<xsl:value-of select="concat(., '"')"/>
<xsl:if test="count(../following-sibling::communication) +
count(following-sibling::*)!=0">
<xsl:value-of select="concat( ', ', '"')"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>