如何在 xslt 的选择语句中使用 Continue

How can I use Continue in a choose statement in xslt

如果满足第一个,我想继续检查其他 if 语句。据我所知,Choose 语句不像其他语言那样具有此功能。 One post suggested 到 运行 多个 if 语句,但这不起作用。在遇到第一个 if 语句后它就停在那里,我希望它保持 运行ning.

我怎样才能做到这一点? 这是我的代码:

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

如果我理解你的 real problem 正确,你想做的很简单:

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>

应用于合理的测试输入:

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>

这将 return:

结果

{ 
    "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."
        }  
    ] 
} 

如您所见,xsl:choosexsl:if 都不需要。但是,您可能希望通过将文字文本放在 xsl:text 块中来使其更易于管理。

另请注意,JSON中还有其他保留字符需要转义。

谢谢@michael.hor257k 的帮助。 我就是这样去做的

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

结果:

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