html - xslt中列表元素内的分隔斜体和跨度标签
问题描述
我是 XSLT 世界的新手,提前感谢您的理解。我需要准备一个将发送到 Adobe InDesign 服务器的 xml。在 html 文件中,这是我需要转换为 xml 并使用 XSLT 转换发送到 Adobe InDesign 的输入,我有“li”元素,其中包含“span”标签和“i”(斜体)标签。我想将“i”标签处理为 InDesign 的最终 xml 中的斜体。我尝试通过以下 xslt 匹配“i”标签:
<xsl:template match="i" mode="process-text">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>
<xsl:copy-of select="text()"/>
</Content>
</CharacterStyleRange>
</xsl:template>
但没有结果。
例如,我有以下输入:
<li class="MsoNormal" style="mso-list:l0 level2 lfo1;tab-stops:list 1.0in">Systolic dysfunction: an <i>inotropic</i> abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction <45%).</li>
我想将其转换为以下内容:
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL2">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>Systolic dysfunction: an </Content>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>inotropic</Content>
</CharacterStyleRange>
<Content> abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction <45%).</Content>
<Br/>
</CharacterStyleRange>
</ParagraphStyleRange>
我最初的问题是如何拆分“li”标签并(单独)处理其中的文本,以及如何通过 XSLT 分别处理“li”中的“span”和“i”标签?预先感谢您的任何帮助。
更新: 我的主要模板,“li”元素是:
<xsl:template match="li[not(descendant::p) and not(ancestor::section[@class='references' or @class='References'])]" mode="li-pass1">
<xsl:variable name="depth" select="count(ancestor::li) + 1"/>
<xsl:variable name="listType">
<xsl:choose>
<xsl:when test="parent::ol">
<xsl:value-of select="'NL'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'BL'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/{$listType}{if ($depth eq 1) then '' else $depth}">
<xsl:choose>
<xsl:when test="descendant::i/text()">
<Content>
<xsl:copy-of select="./text() | descendant::span/text() "/>
</Content>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>
<xsl:copy-of select="descendant::i/text()"/>
</Content>
</CharacterStyleRange>
</xsl:when>
<xsl:otherwise>
<Content>
<xsl:copy-of select="./text() | descendant::span/text() "/>
</Content>
</xsl:otherwise>
</xsl:choose>
</ParagraphStyleRange>
</xsl:template>
此模板以错误的方式影响最终的 xml。我得到以下结果:
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>Two potential pathophysiologic conditions lead to the clinical findings of HF, namely systolic and/or diastolic heart dysfunction.
</Content>
</CharacterStyleRange>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>inotropiccompliance</Content>
</CharacterStyleRange>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"/>
</ParagraphStyleRange>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL2">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>Systolic dysfunction: an abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction <45%).</Content>
</CharacterStyleRange>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>inotropic</Content>
</CharacterStyleRange>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"/>
</ParagraphStyleRange>
因此,您可以看到,斜体元素位于单独的标签中,但没有其他内容。你能建议我需要做什么吗?
解决方案
我会尝试编写模板,将每种元素类型映射到相应的结果结构并在内部使用<xsl:apply-templates/>
以保持处理。所以该样本的基本方法看起来像
<xsl:template match="li">
<xsl:variable name="depth" select="count(ancestor::li) + 1"/>
<xsl:variable name="listType">
<xsl:choose>
<xsl:when test="parent::ol">
<xsl:value-of select="'NL'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'BL'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/{$listType}{if ($depth eq 1) then '' else $depth}">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<xsl:apply-templates/>
</CharacterStyleRange>
</ParagraphStyleRange>
</xsl:template>
<xsl:template match="i">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>
<xsl:apply-templates/>
</Content>
</CharacterStyleRange>
</xsl:template>
<xsl:template match="text()[normalize-space()]">
<Content>
<xsl:value-of select="."/>
</Content>
</xsl:template>
https://xsltfiddle.liberty-development.net/93dFK9Q
这给了
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL">
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
<Content>Systolic dysfunction: an </Content>
<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
<Content>
<Content>inotropic</Content>
</Content>
</CharacterStyleRange>
<Content> abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction <45%).</Content>
</CharacterStyleRange>
</ParagraphStyleRange>
我可能没有捕获您需要的输出格式的所有详细信息,但我希望示例显示关键是用于apply-templates
处理具有匹配模板的子节点。
推荐阅读
- ruby-on-rails - resque-web 作为独立的应用程序来监控具有 rails api only 应用程序的工作人员
- textbox - 如何使用文本框/消息框(VS2017)将总数变为 2 位小数
- ios - '没有帐户'LXxxxxxxx'' Xcode 9.4 上的构建错误
- regex - 嵌套和混合前瞻和后瞻?
- python - 我如何在kivy中停止过渡到下一个屏幕
- python - 如何在 pandas 数据框中使用 ast.literal_eval 并处理异常
- ios - iOS 应用因第三方品牌而被拒绝
- swift - 使 Vapor API 响应 JSON API Spec 兼容
- amazon-ec2 - Wildfly 10 服务启动失败 - Centos7 | AWS EC2
- json - 在具有 ID 的 Python 3 中将 JSON 转换为 Dataframe