本文介绍了xslt 中的条件检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我上一个问题中的一个小更新已经@Dimetre 回答了
A Little update in my previous question already @Dimetre answered that
输入 XML
<tutorial>
<lessons>
<lesson>
chapter Bat 20
</lesson>
<lesson>
chapter Pen Ball 10~
</lesson>
<lesson>
chapter Book
</lesson>
<lesson>
note lesson
</lesson>
<lessons1>
<lesson>
chapter Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
chapter Car Tank 25
</lesson>
</lessons1>
</lessons>
输出将是
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>chapter</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<sample>
<original>note lesson</original>
</sample>
</toc1>
<toc2>
<toc>
<chapter>chapter</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<sample>
<original>description page</original>
</sample>
<toc>
<chapter>chapter</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
在输入 XML 中,我已经为示例编写了两个课程(课程,第 1 课),但实际上我有 n 节课.我想我要求更多,但我正在同时学习.
Here in input XML I have for sample i have written two Lessons (lesson, Lesson1) actually but actually it my have n number of lessons. I think I am asking more but i am learning simultaneously.
请帮帮我&指导我
Please help me & Guide me
提前致谢
问候卡西克
推荐答案
这种转变:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="tutorial">
<Geography>
<historical>
<social>
<xsl:apply-templates select=
"*[starts-with(name(),'lessons')]"/>
</social>
</historical>
</Geography>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'lessons')]">
<xsl:variable name="vPos" select="position()"/>
<xsl:element name="toc{$vPos}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
<xsl:variable name="vNorm" select=
"translate(normalize-space(), '~', '')"/>
<xsl:variable name="vAtUnit" select=
"substring-after($vNorm, 'chapter')"/>
<xsl:variable name="vUnit" select=
"replace($vAtUnit, '([^0123456789]+)(\d*)', '$1')"/>
<xsl:variable name="vLastPart" as="xs:string" select=
"substring-after($vAtUnit, $vUnit)"/>
<xsl:variable name="vNum"
select="concat($vLastPart, '10'[not($vLastPart)])"/>
<toc>
<chapter>chapter</chapter>
<unit><xsl:value-of select="normalize-space($vUnit)"/></unit>
<pages><xsl:value-of select="$vNum"/></pages>
</toc>
</xsl:template>
<xsl:template match="lesson">
<toc>
<sample>
<original><xsl:value-of select="normalize-space()"/></original>
</sample>
</toc>
</xsl:template>
</xsl:stylesheet>
当应用于提供的 XML 文档时(更正为没有将 lessons1
嵌套在 lessons
中):
when applied on the provided XML document (corrected not to have lessons1
nested in lessons
):
<tutorial>
<lessons>
<lesson>
chapter Bat 20
</lesson>
<lesson>
chapter Pen Ball 10~
</lesson>
<lesson>
chapter Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
chapter Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
chapter Car Tank 25
</lesson>
</lessons1>
</tutorial>
产生想要的、正确的结果:
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>chapter</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>chapter</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>chapter</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
</historical>
</Geography>
这篇关于xslt 中的条件检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!