Home > front end >  How can I use Continue in a choose statement in xslt
How can I use Continue in a choose statement in xslt

Time:05-08

I want to continue to check for other if statements if the first one is met. From what I read, Choose statement does not have this functionality like other languages do. One post suggested to just run multiple if statements but this is not working. After the first if statement is met it stops there, I want it to keep running.

How can I achieve this? This is my code:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="root"> 
    { 
        
        "entityList": 
        [
            <xsl:for-each select="//entityList"> 
            {
                "notes":"<xsl:call-template name="replace">
                <xsl:with-param name="text" select="./Notes"/>
            </xsl:call-template>",
            } <xsl:if test="position() != last()">,</xsl:if>
        </xsl:for-each> 
        ] 
    } 
    </xsl:template>
    <xsl:template name="replace">
        <xsl:param name="text"/>
        <xsl:param name="searchString">"</xsl:param>
        <xsl:param name="replaceString">\"</xsl:param>
        <xsl:param name="searchStringSpace">&#x9;</xsl:param>
        <xsl:param name="searchStringBackslash">\</xsl:param>
        <xsl:if test="contains($text,$searchString)">
          <xsl:value-of select="replace(./Notes,'&quot;','\\&quot;')"/>
        </xsl:if>
        <xsl:if test="contains($text,$searchStringSpace)">
          <xsl:value-of select="replace(./Notes,'&#x9;','\\t')"/> 
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

<root>
    <totalRecords>429</totalRecords>
    <offset>0</offset>
    <entityList>
        <Notes>Dear Deans, Chairs and &quot;Directors,There was a &#x9;ANDOR question about scheduling Mac Hall Auditorium,for regular classes and policy/procedure around it.,Mac Hall Auditorium was intended to be available as a,regular classroom. That is why the seats have built-in mini,desks.,Your programs are welcome to schedule classes in,Mac Auditorium  - you can request it when submitting your,course schedules for the Semester or through the Schedule,Change Request Form.,There are, however, some conditions:,1)faculty members who teach in Mac Auditorium should,anticipate that their class may be displaced if the,University-wide function (president?s address, a conference,,etc.) is scheduled at the time of their class. It would be,up to the faculty to either reschedule that,class meeting for a different time, find another room/space,or substitute the class meeting with an alternative activity,There is no easy way to inform faculty about the upcoming,events, so they would have to watch University Calendar to,determine when their classes might be affected.,2)Mac Hall will be unavailable for scheduling regular,courses during evening hours (6-10 pm) ? that time will be,set aside for Pegasus Players practices.,Natalia F. Blank, Ph.D. 2/17/21</Notes>
        
    </entityList>
    <totalPages>1</totalPages>
    <page>0</page>
    <status>success</status>
</root>

CodePudding user response:

If I understand your real problem correctly, you want to do simply:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text' encoding='utf-8' />

<xsl:template match="root"> 
    { 
        "entityList": 
        [
    <xsl:for-each select="entityList"> 
            {
                "notes":"<xsl:value-of select="replace(replace(Notes,'&quot;','\\&quot;'), '&#x9;','\\t')"/>"
            } <xsl:if test="position() != last()">,</xsl:if>
    </xsl:for-each> 
        ] 
    } 
</xsl:template>
    
</xsl:stylesheet>

Applied to a sensible test input:

XML

<root>
    <totalRecords>429</totalRecords>
    <offset>0</offset>
    <entityList>
        <Notes>This note has no reserved characters.</Notes>
    </entityList>    
   <entityList>
        <Notes>This note has &quot;quoted text&quot; in it.</Notes>
    </entityList>    
     <entityList>
        <Notes>Here comes&#x9;a tab character</Notes>
    </entityList> 
    <entityList>
        <Notes>This note has both &quot;quoted text&quot; and&#x9;a tab.</Notes>
    </entityList>    
    <totalPages>1</totalPages>
    <page>0</page>
    <status>success</status>
</root>

This will return:

Result

{ 
    "entityList": 
    [
     
        {
            "notes":"This note has no reserved characters."
        } , 
        {
            "notes":"This note has \"quoted text\" in it."
        } , 
        {
            "notes":"Here comes\ta tab character"
        } , 
        {
            "notes":"This note has both \"quoted text\" and\ta tab."
        }  
    ] 
} 

As you can see, there is no need for neither xsl:choose nor xsl:if. However, you might want to make this more manageable by putting the literal texts inside xsl:text blocks.

Note also that there are other reserved characters that need to be escaped in JSON.

CodePudding user response:

Thank you @michael.hor257k for you help. This is how I went about it

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="root"> 
    { 
        
        "entityList": 
        [
            <xsl:for-each select="//entityList"> 
            {
                "notes":"<xsl:call-template name="replace">
                <xsl:with-param name="text" select="./Notes"/>
            </xsl:call-template>",
            } <xsl:if test="position() != last()">,</xsl:if>
        </xsl:for-each> 
        ] 
    } 
    </xsl:template>
    <xsl:template name="replace">
        <xsl:param name="text"/>
        <xsl:value-of select="replace(replace(replace($text,'\&#92;','\\\\'),'&quot;','\\&quot;'),'&#x9;','\\t')"/>
    </xsl:template>
</xsl:stylesheet>

RESULT:

{
    "entityList": [
        {
            "notes": "Dear Deans, \\Chairs and \"Directors,There was a \tANDOR question about scheduling Mac Hall Auditorium."
        }
    ]
}

  • Related