上一篇文章。
这是链接
再次输入xml中的小更新,其他验证都是相同的。这里只有章节(元素)正在改变而不是章节我将有数字
<tutorial>
<lessons>
<lesson>
12000 Bat 20
</lesson>
<lesson>
15000 Pen Ball 10~
</lesson>
<lesson>
14000 Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
24000 Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
8000 Car Tank 25
</lesson>
</lessons1>
在上一个问题中我们有章节是第一个节点(第20章章节),但在这里我有12000个蝙蝠20
上述输入的欲望输出是
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>12000</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>15000</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>14000</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>24000</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>8000</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
@Dimitre&amp; @Tomalak从下次开始,我将编写完全准备好的问题,我肯定会用解决方案发布它,现在我开始学习更快(XSLT)以下输出和以前的输出。
请在这里指导我
提前致谢 Karthic
答案 0 :(得分:1)
此转化:
<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[substring-before(normalize-space(), ' ')
castable as xs:integer
]">
<xsl:variable name="vNorm" select=
"translate(normalize-space(), '~', '')"/>
<xsl:variable name="vAtUnit" select=
"substring-after($vNorm, ' ')"/>
<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>
<xsl:value-of select="substring-before($vNorm, ' ')"/>
</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文档:
<tutorial>
<lessons>
<lesson>
12000 Bat 20
</lesson>
<lesson>
15000 Pen Ball 10~
</lesson>
<lesson>
14000 Book
</lesson>
<lesson>
note lesson
</lesson>
</lessons>
<lessons1>
<lesson>
24000 Pencil 10
</lesson>
<lesson>
description page
</lesson>
<lesson>
8000 Car Tank 25
</lesson>
</lessons1>
</tutorial>
生成想要的正确结果:
<Geography>
<historical>
<social>
<toc1>
<toc>
<chapter>12000</chapter>
<unit>Bat</unit>
<pages>20</pages>
</toc>
<toc>
<chapter>15000</chapter>
<unit>Pen Ball</unit>
<pages>10</pages>
</toc>
<toc>
<chapter>14000</chapter>
<unit>Book</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>note lesson</original>
</sample>
</toc>
</toc1>
<toc2>
<toc>
<chapter>24000</chapter>
<unit>Pencil</unit>
<pages>10</pages>
</toc>
<toc>
<sample>
<original>description page</original>
</sample>
</toc>
<toc>
<chapter>8000</chapter>
<unit>Car Tank</unit>
<pages>25</pages>
</toc>
</toc2>
</social>
</historical>
</Geography>