XSLT 转换、字符串连接
我有下面的 xml
<R N="14" MIME="application/pdf">
<RK>7</RK>
<MT N="Abstract"
V="Lorem Ipsum is simply dummy text of the printing " />
<MT N="Abstract1"
V="and typesetting industry. Lorem Ipsum has been the industry's standard "/>
<MT N="Author" V="Bernard Shaw;" />
<MT N="Author1" V="Mark Twain" />
<MT N="Abstract2"
V="dummy text ever since the 1500s, when an unknown printer took a galley"/>
<LANG>en</LANG>
</R>
当使用 XSLT 进行转换时,我需要连接 Abstract 和 Author 字段,并显示为
Abstract: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
Author: Bernard Shaw;Mark Twain
Abstract、Abstract1、Abstract2 可能以任意顺序出现在 xml 中。
我正在尝试使用如下所示的内容,但坚持条件 &当 Abstract、Abstract1 不以相同顺序出现时字符串的串联
<xsl:template match="MT">
<xsl:if test="(some generic condition to display Title)">
<br/>
<span class="f">
<xsl:value-of select="@N"/>
</span>
</xsl:if>
<xsl:value-of select="@V"/>
</xsl:template>
感谢任何帮助。
I have the below xml
<R N="14" MIME="application/pdf">
<RK>7</RK>
<MT N="Abstract"
V="Lorem Ipsum is simply dummy text of the printing " />
<MT N="Abstract1"
V="and typesetting industry. Lorem Ipsum has been the industry's standard "/>
<MT N="Author" V="Bernard Shaw;" />
<MT N="Author1" V="Mark Twain" />
<MT N="Abstract2"
V="dummy text ever since the 1500s, when an unknown printer took a galley"/>
<LANG>en</LANG>
</R>
When transforming with an XSLT, I need to concatenate the Abstract and Author fields, and show it like
Abstract: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
Author: Bernard Shaw;Mark Twain
The Abstract, Abstract1, Abstract2 may appear in any order in the xml.
I am trying to use something like below, but stuck on the condition & concatenation of string when Abstract, Abstract1 doesn't appear in the same order
<xsl:template match="MT">
<xsl:if test="(some generic condition to display Title)">
<br/>
<span class="f">
<xsl:value-of select="@N"/>
</span>
</xsl:if>
<xsl:value-of select="@V"/>
</xsl:template>
Appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此转换:
应用于提供的 XML 文档时:
产生所需的结果:
进一步重构:
第二次重构(OP 请求):
编辑:OP 请求“所有处理都应在匹配
MT
的模板内完成”。虽然这在简单的情况下是可能的(参见 @Alejandro 的答案),但在匹配MT
的单个模板中进行所有处理会打开巨大的未知空白。例如,可能需要以不同的方式处理其他MT
元素,在这种情况下,根本不会进行此类处理。在更复杂的情况下(例如当元素和属性以任意顺序出现时,但输出必须排序 - Abstract1、Abstract2、...、Abstract-N),则必须显式指定排序,并且必须位于匹配
MT
的模板之外。 因此,一般情况下,仅在模板匹配MT
内的代码不可能产生所需的输出。我建议使用单个模板匹配
MT
处于命名模式,并且应该以“拉式风格”使用——由调用者显式应用。This transformation:
when applied on the provided XML document:
produces the wanted result:
Further refactoring:
A second refactoring (requested by the OP):
EDIT: The OP has requested that "all processing should be done within the template matching
MT
". While this is possible in simple cases (see @Alejandro's answer), doing all the processing in a single template matchingMT
opens big gaps of unknowns. For example, it may be required to process otherMT
elements in a different way and in this case such processing will not be done at all.In more complex cases (such as when the elements and attributes come in any order, but the output must be sorted -- Abstract1, Abstract2, ..., Abstract-N), then the sort has to be specified explicitly and this has to be outside the template matching
MT
. Therefore, in the general case, it is not possible to produce the required output with code only within the template matchingMT
.I would recommend that the single template matching
MT
be in a named mode and that it should be used in "pull style" -- applied explicitly by the caller.此样式表:
输出:
注意:按@N字母内容和父生成的id分组,因为我期望有几个
R
元素。This stylesheet:
Output:
Note: Grouping by @N alphabetic content and parent generated id because I would expect several
R
elements.