如何在xslt中的select语句中使用Continue



如果满足第一个if语句,我想继续检查其他if语句。据我所知,Choose语句不像其他语言那样具有此功能。一篇文章建议只运行多个if语句,但这不起作用。在满足第一个if语句后,它就停止了,我希望它继续运行。

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

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

如果我正确理解你的真实问题,你只想简单地做:

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>

这将返回:

结果

{ 
"entityList": 
[

{
"notes":"This note has no reserved characters."
} , 
{
"notes":"This note has "quoted text" in it."
} , 
{
"notes":"Here comesta tab character"
} , 
{
"notes":"This note has both "quoted text" andta 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."
}
]
}

最新更新