0

I am new to working with xsl. I need to loop through some xml elements : "telecom" and for each one I need to search if it contains tel if it does assign it to PtPhoneNumber variable. Then if it contains mailto assign that to PtEmail.

Here is the XML:

<recordTarget>
<patientRole>
<telecom use="HP" value="tel:555-555-2004"></telecom>
<telecom use="HP" value="mailto:aaeveryman@email.com"></telecom>

Here is my XPath for loop and if statements:

    <xsl:variable name="Telecom" select="ClinicalDocument/recordTarget/patientRole/telecom/@value"/>
<xsl:for-each select="Telecom">"
    <xsl:if test = "contains(Telecom,'tel')">
        <xsl:variable name="PtPhoneNumber"/>
    </xsl:if>
    <!--<xsl:if test=(fn:contains('mail',''))>
        <xsl:variable name="PtEmail"/>
    </xsl:if>-->
</xsl:for-each>

Wondering if someone can help me why this syntax and if it is possible

Amanda
  • 31
  • 5

2 Answers2

0

If you are trying to set these variables with the values of the telecom/@value that contain "tel" and "mailto", then you don't want to use an xsl:for-each. Assigning the variable inside of the xsl:for-each would mean that they would immediately go out of scope on the next iteration of the for loop.

You can apply a predicate to select the telecom/@value that matches the contains criteria, and select it's value:

<xsl:variable name="Telecom" select="ClinicalDocument/recordTarget/patientRole/telecom/@value"/>
<xsl:variable name="PtPhoneNumber" select="$Telecom[contains(., 'tel')]"/>
<xsl:variable name="PtEmail" select="$Telecom[contains(., 'mailto')]"/>

Your sample data only had one of each. If there happened to be more than one "tel" or "mailto" value, you could select the first one by adding another predicate to select the first of those matches:

<xsl:variable name="PtPhoneNumber" select="$Telecom[contains(., 'tel')][1]"/>
<xsl:variable name="PtEmail" select="$Telecom[contains(., 'mailto')][1]"/>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Thank you! I appreciate the advice on the for-each loop. I have a programming background and I have learned that I can't think like that. Thank you so much this makes sense and worked! – Amanda Oct 11 '18 at 15:06
0

You've given a very procedural description of the processing steps you want to perform, whereas what we really need is a description of the output you want to produce and how it relates to the input. XSLT is a functional language, and in a functional language, you don't instruct the computer to perform loops and assign values to variables, rather you describe the output as a function of the input. So I'm afraid if you come from a background in procedural languages then you have to think in a rather different way from the way you are used to.

You have tried to write procedural code and it isn't going to work. Reverse engineering your requirements from code that doesn't work isn't easy. Show us the required output instead!

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • You are correct I have a programming background so I have learned that this is a new way of thinking. Thanks for the advice – Amanda Oct 11 '18 at 15:05