【发布时间】:2014-08-26 06:55:46
【问题描述】:
我必须将平面 xml 转换为分层 xml。 我不知道这个任务。 下面是转换的输入。
输入:-
<body>
<p class="title">Article Title</p>
<p class="Authors">abc, pqr and xyz</p>
<p class="intro">here is introdution text......</p>
<p class="head1">1: Heading level 1</p>
<p>some text here</p>
<p>some text here</p>
<p class="head2">1.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">1.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head1">2: Heading level 1</p>
<p class="head2">2.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.2: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
</body>
我想把它转换成下面给定的xml。
输出:-
<article>
<title>Article Title</title>
<Authors>abc, pqr and xyz</Authors>
<introduction>here is introdution text......</introduction>
<body>
<sec level="1">
<title>1: Heading level 1</title>
<p>some text here</p>
<p>some text here</p>
<sec level="2">
<title>1.1: Heading level 2</title>
<p>some text here</p>
<p>some text here</p>
<sec level="3">
<title>1.1.1: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
</sec>
</sec>
<sec level="1">
<title>2: Heading level 1</title>
<sec level="2">
<title>2.1: Heading level 2</title>
<p>some text here</p>
<p>some text here</p>
<sec level="3">
<title>2.1.1: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
<sec level="3">
<title>2.1.2: Heading level 3</title>
<p>some text here</p>
<p>some text here</p>
</sec>
</sec>
</sec>
</body>
</article>
我不知道使用 xslt 转换它。
我低于输出:-
<article>
<p class="title">Article Title</p>
<p class="Authors">abc, pqr and xyz</p>
<p class="intro">here is introdution text......</p>
<body>
<p class="head1">1: Heading level 1</p>
<p>some text here</p>
<p>some text here</p>
<p class="head2">1.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">1.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head1">2: Heading level 1</p>
<p class="head2">2.1: Heading level 2</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.1: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
<p class="head3">2.1.2: Heading level 3</p>
<p>some text here</p>
<p>some text here</p>
</body>
</article>
我的代码是:-
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="html">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="head"/>
<xsl:template match="body">
<xsl:element name="article">
<xsl:apply-templates select="p[@class='title']|p[@class='Authors']|p[@class='intro']"/>
<xsl:element name="body">
<xsl:apply-templates select="p[preceding-sibling::p[@class='intro']]"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
您认为向“不知道”如何做的人提供答案是否具有很高的教育价值?尝试解决您的问题 - 或者至少在 Google 上搜索您的问题的标题。
-
@LuvKS 请不要发表个人言论。
-
请参阅stackoverflow.com/questions/9175991/… 了解类似问题的 XSLT 2.0 解决方案,基本上您需要使用
for-each-group select="*" group-starting-with="p[@class = concat('head', $level)]"编写递归函数或模板。