如何在单个 xml 节点上 运行 两个单独的 xslt 子字符串操作
How to run two separate xslt substring operations on a single xml node
我有一段由数据库查询生成的带有 xml 标记的文本,(我认为)在 /record/scope/scope 中,我需要对其执行两个操作:
1) 将换行符转换为 <br /
> 标签
2) 使用结果输出,检查整个字符串是否超过 350 个字符。如果是,则显示前 350 个字符,并在其后显示更多 link;单击 Show More link 时,应显示整个文本(包括中断),最后应显示 Show Less link。单击 Show Less 时,应仅显示 350 个字符。 (如果不超过 350 个字符,则整个块应该显示而不会显示 More/Show Less links。)
我可以分别做这些事情中的每一个(使用别人写的代码)但是我不知道如何结合这两个操作。
我试过将第一个操作的输出(这是一个调用模板来编写别人编写的代码)到一个变量中,但是我不知道如何 运行 第二个操作那个变量。
这是 xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adlibXML>
<recordList>
<record priref="547" created="2016-11-13T12:55:00" modification="2019-09-13T11:48:17" selected="false">
<scope>
<scope>
The Marjorie Best papers span the years 1934-1965 (bulk 1947-1965) and encompass 3.8 linear feet. The collection consists primarily of script material, wardrobe plots, and costume sketches. Eight of the scripts are signed by the cast and several are annotated. Wardrobe plots, many with cloth swatches attached, document Best's work on more than 30 films. The document boxes contain more than 40 costume sketches, including signed sketches for "Look for the Silver Lining" (1948).
Three flat boxes contain oversize costume sketches for 32 films from 1938 to 1965, including "The Adventures of Don Juan" (1949), "The Adventures of Marco Polo" (1938), "Giant" (1956), "The Greatest Story Ever Told" (1965), "Rio Bravo" (1959), "Sunrise at Campobello" (1960), "That Hamilton Woman!" (1941), and "The Time, the Place and the Girl" (1947).
</scope>
</scope>
</record>
</recordList>
</adlibXML>
这是 xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL /Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" />
<xsl:template match="record">
<xsl:call-template name="record" />
</xsl:template>
<xsl:template name="record">
<!-- Scope notes -->
<xsl:apply-templates select="scope/scope" />
</xsl:template>
<!-- This template and the next one together insert <br /> in place of a line break. -->
<xsl:template match="scope">
<xsl:variable name="formatted">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$formatted"/>
</xsl:template>
<xsl:template name="replace-newline">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<p>
<xsl:value-of select="substring-before($text, '
')"/>
<br />
</p>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring-after($text, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- This template checks the length of the content and puts the links if necessary. -->
<xsl:template match="scope">
<xsl:choose>
<xsl:when test="string-length(.) > 355" >
<xsl:value-of select="substring(., 1, 355 + string-length(substring-before(substring(., 355),' ')))" />
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br />
</div>
<div class="toggle-group children">
<xsl:value-of select="substring(., 355 + string-length(substring-before(substring(., 355),' ')))" />
</div>
<div class="toggle-group hide-label script">
<a> Show less...</a>
</div>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我不认为我可以给出一个真实的工作示例,因为有太多的依赖项我不能分享,但我认为基本上 HTML 看起来像这样:
<p>The Marjorie Best papers span the years 1934-1965 (bulk 1947-1965) and encompass 3.8 linear feet. The collection consists primarily of script material, wardrobe plots, and costume sketches. Eight of the scripts are signed by the cast and several are annotated. Wardrobe plots, many with cloth swatches attached, document Best's work on more than 30 films.
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br></div>
<div class="toggle-group children"> The document boxes contain more than 40 costume sketches, including signed sketches for "Look for the Silver Lining" (1948).
<br/>
</p>Three flat boxes contain oversize costume sketches for 32 films from 1938 to 1965, including "The Adventures of Don Juan" (1949), "The Adventures of Marco Polo" (1938), "Giant" (1956), "The Greatest Story Ever Told" (1965), "Rio Bravo" (1959), "Sunrise at Campobello" (1960), "That Hamilton Woman!" (1941), and "The Time, the Place and the Girl" (1947).</div>
<div class="toggle-group hide-label script">
<a> Show less...</a></div>
</div>
很难理解确切的输出应该是什么。以下示例显示了如何使用一些任意假设来组合这两个操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/adlibXML">
<html>
<body>
<xsl:apply-templates select="recordList/record/scope/scope" />
</body>
</html>
</xsl:template>
<xsl:template match="scope">
<div>
<xsl:choose>
<xsl:when test="string-length(.) > 350">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 1, 350)"/>
</xsl:call-template>
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br />
</div>
<div class="toggle-group children">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 351)"/>
</xsl:call-template>
</div>
<div class="toggle-group hide-label script">
<a> Show less...</a>
</div>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 1, 350)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
<xsl:template name="replace-newline">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:value-of select="substring-before($text, '
')"/>
<br/>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring-after($text, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我有一段由数据库查询生成的带有 xml 标记的文本,(我认为)在 /record/scope/scope 中,我需要对其执行两个操作:
1) 将换行符转换为 <br /
> 标签
2) 使用结果输出,检查整个字符串是否超过 350 个字符。如果是,则显示前 350 个字符,并在其后显示更多 link;单击 Show More link 时,应显示整个文本(包括中断),最后应显示 Show Less link。单击 Show Less 时,应仅显示 350 个字符。 (如果不超过 350 个字符,则整个块应该显示而不会显示 More/Show Less links。)
我可以分别做这些事情中的每一个(使用别人写的代码)但是我不知道如何结合这两个操作。
我试过将第一个操作的输出(这是一个调用模板来编写别人编写的代码)到一个变量中,但是我不知道如何 运行 第二个操作那个变量。
这是 xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adlibXML>
<recordList>
<record priref="547" created="2016-11-13T12:55:00" modification="2019-09-13T11:48:17" selected="false">
<scope>
<scope>
The Marjorie Best papers span the years 1934-1965 (bulk 1947-1965) and encompass 3.8 linear feet. The collection consists primarily of script material, wardrobe plots, and costume sketches. Eight of the scripts are signed by the cast and several are annotated. Wardrobe plots, many with cloth swatches attached, document Best's work on more than 30 films. The document boxes contain more than 40 costume sketches, including signed sketches for "Look for the Silver Lining" (1948).
Three flat boxes contain oversize costume sketches for 32 films from 1938 to 1965, including "The Adventures of Don Juan" (1949), "The Adventures of Marco Polo" (1938), "Giant" (1956), "The Greatest Story Ever Told" (1965), "Rio Bravo" (1959), "Sunrise at Campobello" (1960), "That Hamilton Woman!" (1941), and "The Time, the Place and the Girl" (1947).
</scope>
</scope>
</record>
</recordList>
</adlibXML>
这是 xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL /Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" />
<xsl:template match="record">
<xsl:call-template name="record" />
</xsl:template>
<xsl:template name="record">
<!-- Scope notes -->
<xsl:apply-templates select="scope/scope" />
</xsl:template>
<!-- This template and the next one together insert <br /> in place of a line break. -->
<xsl:template match="scope">
<xsl:variable name="formatted">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$formatted"/>
</xsl:template>
<xsl:template name="replace-newline">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<p>
<xsl:value-of select="substring-before($text, '
')"/>
<br />
</p>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring-after($text, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- This template checks the length of the content and puts the links if necessary. -->
<xsl:template match="scope">
<xsl:choose>
<xsl:when test="string-length(.) > 355" >
<xsl:value-of select="substring(., 1, 355 + string-length(substring-before(substring(., 355),' ')))" />
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br />
</div>
<div class="toggle-group children">
<xsl:value-of select="substring(., 355 + string-length(substring-before(substring(., 355),' ')))" />
</div>
<div class="toggle-group hide-label script">
<a> Show less...</a>
</div>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我不认为我可以给出一个真实的工作示例,因为有太多的依赖项我不能分享,但我认为基本上 HTML 看起来像这样:
<p>The Marjorie Best papers span the years 1934-1965 (bulk 1947-1965) and encompass 3.8 linear feet. The collection consists primarily of script material, wardrobe plots, and costume sketches. Eight of the scripts are signed by the cast and several are annotated. Wardrobe plots, many with cloth swatches attached, document Best's work on more than 30 films.
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br></div>
<div class="toggle-group children"> The document boxes contain more than 40 costume sketches, including signed sketches for "Look for the Silver Lining" (1948).
<br/>
</p>Three flat boxes contain oversize costume sketches for 32 films from 1938 to 1965, including "The Adventures of Don Juan" (1949), "The Adventures of Marco Polo" (1938), "Giant" (1956), "The Greatest Story Ever Told" (1965), "Rio Bravo" (1959), "Sunrise at Campobello" (1960), "That Hamilton Woman!" (1941), and "The Time, the Place and the Girl" (1947).</div>
<div class="toggle-group hide-label script">
<a> Show less...</a></div>
</div>
很难理解确切的输出应该是什么。以下示例显示了如何使用一些任意假设来组合这两个操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/adlibXML">
<html>
<body>
<xsl:apply-templates select="recordList/record/scope/scope" />
</body>
</html>
</xsl:template>
<xsl:template match="scope">
<div>
<xsl:choose>
<xsl:when test="string-length(.) > 350">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 1, 350)"/>
</xsl:call-template>
<div class="toggle-group">
<div class="toggle-group show-label script">
<a>Show more...</a>
<br />
</div>
<div class="toggle-group children">
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 351)"/>
</xsl:call-template>
</div>
<div class="toggle-group hide-label script">
<a> Show less...</a>
</div>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring(., 1, 350)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:template>
<xsl:template name="replace-newline">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:value-of select="substring-before($text, '
')"/>
<br/>
<xsl:call-template name="replace-newline">
<xsl:with-param name="text" select="substring-after($text, '
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>