如果滿足第一個,我想繼續檢查其他 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">	</xsl:param>
<xsl:param name="searchStringBackslash">\</xsl:param>
<xsl:if test="contains($text,$searchString)">
<xsl:value-of select="replace(./Notes,'"','\\"')"/>
</xsl:if>
<xsl:if test="contains($text,$searchStringSpace)">
<xsl:value-of select="replace(./Notes,'	','\\t')"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<root>
<totalRecords>429</totalRecords>
<offset>0</offset>
<entityList>
<Notes>Dear Deans, Chairs and "Directors,There was a 	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>
uj5u.com熱心網友回復:
如果我正確理解您的真正問題,您只想簡單地做:
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,'"','\\"'), '	','\\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 "quoted text" in it.</Notes>
</entityList>
<entityList>
<Notes>Here comes	a tab character</Notes>
</entityList>
<entityList>
<Notes>This note has both "quoted text" and	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 comes\ta tab character"
} ,
{
"notes":"This note has both \"quoted text\" and\ta tab."
}
]
}
如您所見,既不需要xsl:choose也不需要xsl:if。xsl:text但是,您可能希望通過將文字文本放入塊中來使其更易于管理。
另請注意,還有其他保留字符需要在 JSON 中進行轉義。
uj5u.com熱心網友回復:
謝謝@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,'\\','\\\\'),'"','\\"'),'	','\\t')"/>
</xsl:template>
</xsl:stylesheet>
結果:
{
"entityList": [
{
"notes": "Dear Deans, \\Chairs and \"Directors,There was a \tANDOR question about scheduling Mac Hall Auditorium."
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471878.html
下一篇:無法對沒有年份的員工串列進行分組
