I’m very new to XSLT but have some programming experience. I’m finding how XSL works pretty weird sometimes, but enjoying the challenge. At
this stage I just need some advice to point me in the right direction so I can research for myself and the problem I’m describing hasn’t even
occurred yet, but I’m sure it will when we go from ‘testing’ to ‘live’ so I will need to know how to handle it. At the moment I’m hard coding
indexs to get the values and this works fine for nodes I know that won’t change, but some surely will so I need an alternative to my bodgy
hardcoding.
Also, I tried to use global variables for my repetitive ‘"’ and ‘, ‘ code below to make code easier to read but they kept coming up out
of scope (so they were local?). Couldn’t see what I was doing wrong, or where to declare them. Ideally I want to do it at the begining of the
script and be able to call them whenever and wherever I want but this is not urgent.
For example, at the moment I’m using this;
<!-- 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="*" />
<xsl:param name="component"/>
<xsl:template match="/">
<xsl:choose>
<!-- COMPONENT 4 communications -->
<!-- contactID, mediumCode, areaCode, communicationDetails -->
<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(
'"', contactID, '"', ', ', '"',
communications/communication[1]/mediumCode, '"', ', ', '"',
communications/communication[1]/areaCode, '"', ', ', '"',
communications/communication[1]/communicationDetails, '"', '
',
'"', contactID, '"', ', ', '"',
communications/communication[2]/mediumCode, '"', ', ', '"',
communications/communication[2]/areaCode, '"', ', ', '"',
communications/communication[2]/communicationDetails, '"', '
',
'"', contactID, '"', ', ', '"',
communications/communication[3]/mediumCode, '"', ', ', '"',
communications/communication[3]/areaCode, '"', ', ', '"',
communications/communication[3]/communicationDetails, '"', '
')" />
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
On this;
<medicare>
...
<person>
<contactID>5290001890</contactID>
<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>
</person>
<person>
<contactID>4400139361</contactID>
<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>
</person>
...
</medicare>
To get this;
contactID,mediumCode,areaCode,communicationDetails
"5290001890", "T", "02", "92881781"
"5290001890", "E", "", "rabina.smiley@ekit.com"
"5290001890", "M", "", "04290012333"
"4400139361", "T", "07", "49281771"
"4400139361", "E", "", "suzanne.jones2@optus.com"
"4400139361", "M", "", "0404009266"
But I’m worried this will not work if my daily xml file changes to this;
<medicare>
...
<person>
<contactID>5290001890</contactID>
<communications>
<communication>
<mediumCode>E</mediumCode>
<communicationDetails>rabina.smiley@ekit.com</communicationDetails>
</communication>
<communication>
<mediumCode>M</mediumCode>
<communicationDetails>04290012333</communicationDetails>
</communication>
</communications>
</person>
<person>
<contactID>4400139361</contactID>
<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>
</communications>
</person>
...
</medicare>
Or this;
<medicare>
...
<person>
<contactID>5290001890</contactID>
<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>
<communication>
<mediumCode>X</mediumCode>
<areaCode>XX</areaCode>
<communicationDetails>XXXX</communicationDetails>
</communication>
</communications>
</person>
<person>
<contactID>4400139361</contactID>
<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>
</person>
...
</medicare>
This uses the push style so its only applying the template to the nodes which exsist!
you will find once you get to grips with xslt its very powerful!