如何从多个子节点访问文本值?仅从第一个子节点实例获取值

发布于 2024-11-16 13:56:56 字数 31123 浏览 2 评论 0原文

我继承了一个相当丑陋的 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","","[email protected]"
"5290001890","E","","[email protected]"
"4400139361","E","","[email protected]"
"6600027368","E","","[email protected]"

"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)&gt;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,'&#10;')">
      <xsl:value-of select="substring-before($string, '&#10;')" disable-output-escaping="yes"/>
      <br/>
      <xsl:call-template name="nl2br"><xsl:with-param name="string" select="substring-after($string,'&#10;')"/></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>&#xa;</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>&#xa;</xsl:text>
        </xsl:if>
      </xsl:for-each>
    </xsl:when>

    <!-- COMPONENT 4 communications -->
    <!-- contactID, mediumCode, areaCode, communicationDetails -->

    <xsl:variable name="q" select="'&quot;'" />
    <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>&#xa;</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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>

I've inherited a fairly ugly xsl script and have very limited experience with xslt. I was helped a lot from you guys yesterday but still struggling a bit so I'm providing the whole xml and better description of wanted output.

I assume it's something stupid being done by wrapping the whole script in a template and a choose because I keep getting compile errors when I make suggested changes and because I'm flying a bit blind it's clear to me I lack sufficient understanding and time to acquire it so I would sincerely appreciate some help.

At this stage I am only concerned with component 4. Once I nut it out I'm sure I will be able to rewrite the rest of the script.

Output I am getting;

contactID,mediumCode,areaCode,communicationDetails
"0123456789","T","02","62881111"
"5290001890","T","02","92881781"
"4400139361","T","07","49281771"
"6600027368","T","07","48103280"

Output I need;

contactID,mediumCode,areaCode,communicationDetails
"0123456789","T","02","62881111"
"5290001890","T","02","92881781"
"4400139361","T","07","49281771"
"6600027368","T","07","48103280"

"0123456789","E","","[email protected]"
"5290001890","E","","[email protected]"
"4400139361","E","","[email protected]"
"6600027368","E","","[email protected]"

"0123456789","M","","04140012225"
"5290001890","M","","04290012333"
"4400139361","M","","0404009266"
"6600027368","M","","0414003242"

XSL stylesheet:

<!-- 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 data:

<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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-11-23 13:56:56

试试这个(假设您希望每个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函数调用中更容易阅读。

此样式表通过吐出其 person 祖先的 concatID 来处理任何 communication 元素,然后仅应用模板,这将处理它的三个子元素。

下一个模板以相同的方式处理三个子元素,输出一个逗号,后跟引号中的值。

最后一个模板可防止 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>

Try this (assumes you want each mediumCode on a seperate line):

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

I've created global variables for the commas and the quotes, just to make it a little easier to read in the concat function calls.

This stylesheet processes any communication element by spitting out the concatID of it's person ancestor, then just applying templates, which will process it's three child elements.

The next template handles the three child elements in the same way, outputting a comma followed by the value in quotes.

The last template prevents the contactID from outputting on it's own. XSLT processors by default will look for a template for every node in your XML tree in sequential order. Without this template present, it'll resort to the default behavior for the text node within it, which is just to output it as is.

If you want all the communication nodes on one line, you can do this:

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

EDIT: Here's a solution that produces a consistent number of fields in each row in the output:

<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>
‘画卷フ 2024-11-23 13:56:56

我对您的代码进行了一些更改以避免迭代。您只需对 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", "[email protected]", "M", "04140012225"
"5290001890", "T", "02", "92881781", "E", "[email protected]", "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>

I've changed your code a bit to avoid iteration. You can get other data just hard-coding the index of the communication element. I think this is acceptable in your situation. Moreover, I don't know why you are using normalize-space function. If you have no multiple spaces in your input document, you could avoid that.

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

The result is:

"0123456789", "T", "02", "62881111", "E", "[email protected]", "M", "04140012225"
"5290001890", "T", "02", "92881781", "E", "[email protected]", "M", "04290012333"

Here follows a cleaner approach which produces the same kind of output (applicable in the case your use case is really close to the data you show in your question):

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文