XML XSLT ->HTML 转换 - 如何使用 xslt 将 xml 元素文本拆分为点 [英] XML XSLT -> HTML Transformation - How to split xml element text into dot points with xslt
问题描述
我有一个 XML 数据结构,我正在使用 XSLT 将其转换为 HTML.我需要使用 XSLT 和输出将 XML 的 current_teaching 元素中的文本拆分为点(元素内容中的分号表示我希望拆分发生的位置).
I have a XML data structure that I am transforming with XSLT into HTML. I need to split the text in the current_teaching element of my XML into dot points (semi-colons in the element content indicate where I would like the splitting to occur) with XSLT and output.
到目前为止,这就是我所拥有的.
So far this is what I have.
XML/XSLT 文件:
XML/XSLT FILE:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="StaffList.xslt"?>
<StaffList>
<StaffMember>
<title>Dr John Brown</title>
<titledesc>Senior Lecturer, ICU Security Research Institute</titledesc> <!-- example text -->
<telephone>(645) 2545 6988</telephone>
<mobile>04568 6665 666</mobile>
<facsimile>(61 8) 9999 9999</facsimile>
<email>dr.brown@brown.com</email>
<campus>Mountain Range</campus>
<room>18.13</room>
<description>John Brown is a awesome doctor.</description>
<current_teaching>Data Structures; Principles of Distributed Systems; Fundamentals of Software Engineering</current_teaching>
</StaffMember>
</StaffList>
XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="StaffList.css"/>
</head>
<body>
<xsl:for-each select="StaffList/StaffMember">
<h2 id="title"><xsl:value-of select="title"/></h2>
<h3 id="titledesc"><xsl:value-of select="titledesc"/></h3>
<br></br>
<table>
<tr>
<td id="telephone_1">Telephone</td>
<td id="telephone_2"><xsl:value-of select="telephone"/></td>
</tr>
<tr>
<td id="mobile_1">Mobile</td>
<td id="mobile_2"><xsl:value-of select="mobile"/></td>
</tr>
<tr>
<td id="facsimile_1">Facsimile</td>
<td id="facsimile_2"><xsl:value-of select="facsimile"/></td>
</tr>
<tr>
<td id="email_1">Email</td>
<td id="email_2"><xsl:value-of select="email"/></td>
</tr>
<tr>
<td id="campus_1">Campus</td>
<td id="campus_2"><xsl:value-of select="campus"/></td>
</tr>
<tr>
<td id="room_1">Room</td>
<td id="room_2"><xsl:value-of select="room"/></td>
</tr>
</table>
<br></br>
<br></br>
<p><xsl:value-of select="description"/></p>
<br></br>
</xsl:for-each>
</body>
</html>
</xsl:template></xsl:stylesheet>
我进行了一些搜索并已经找到了一个可能的解决方案,但我无法让我的 XSLT 使用它.这是我尝试过的:
I did some searching and already found a possible solution but I could not get my XSLT to work with it. Here is what I tried:
<ul>
<xsl:analyze-string select="string()" regex=".*?[\.;]" flags="sm">
<xsl:matching-substring>
<li><xsl:value-of select="."/></li>
</xsl:matching-substring>
<xsl:non-matching-substring>
<li><xsl:value-of select="."/></li>
</xsl:non-matching-substring>
</xsl:analyze-string>
</ul>
这是在最后一个 br/br 和/xsl:for-上述每个 XSLT 代码之间添加的.
This was added in-between the last br /br and /xsl:for-each of the XSLT code above.
推荐答案
XSLT 1.0 标记化模板可以非常简短,证明
、
和
可以避免:
The XSLT 1.0 tokenizing template can be very short and simple, proving that <xsl:choose>
, <xsl:when>
and <xsl:otherwise>
can be avoided:
<xsl:template match="current_teaching/text()[normalize-space()]" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="normalize-space($pText)">
<li><xsl:value-of select="substring-before(concat($pText, ';'), ';')"/></li>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, ';')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
通常会选择执行,使用代码中的
指令,就像这样:
And it will typically be selected for execution, using an <xsl:apply-templates>
instruction from your code simply like this:
<ul>
<xsl:apply-templates select="current_teaching"/>
</ul>
说明:使用哨兵编程的原则有助于消除该解决方案的复杂性.将一个标记字符 (;
) 附加到字符串,这样就无需分析两种不同的情况——文本包含分隔符和文本不包含分隔符.
Explanation:
What helps eliminate complexity in this solution is using the principle of Sentinel Programming.
One sentinel character (;
) is appended to the string, and this eliminates the need to analyze two separate cases -- when the text contains the delimiter, and when the text doesn't contain the delimiter.
这篇关于XML XSLT ->HTML 转换 - 如何使用 xslt 将 xml 元素文本拆分为点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!