如何将平面xml数据转换为分层数据xml [英] How to convert flat xml data to hierarchical data xml
本文介绍了如何将平面xml数据转换为分层数据xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将平面 xml 转换为分层 xml.我不知道这个任务.以下是转换输入.
I have to convert a flat xml to hierarchical xml. I have no idea for this task. Below is input for conversion.
输入:-
<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.
I want to convert it to below given 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 转换它.
I havn't any idea to convert it using 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>
推荐答案
您可以使用以下 xslt 代码获得预期输出:
You can use the below xslt code for expected output:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:function name="mf:group" as="element(section)*">
<xsl:param name="entries" as="element(p)*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$entries"
group-starting-with="p[@class = concat('head',$level)]">
<xsl:variable name="P_ID" select="generate-id(.)"/>
<section name="{@class}">
<title>
<xsl:value-of select="."/>
</title>
<xsl:if test="following-sibling::p[1][not(@class)]">
<ps>
<xsl:apply-templates
select="following-sibling::p[not(@class)][generate-id(preceding-sibling::p[@class][1]) = $P_ID]"
/>
</ps>
</xsl:if>
<xsl:sequence select="mf:group(current-group() except ., ($level + 1))"/>
</section>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="body">
<xsl:copy>
<xsl:sequence select="mf:group(p[contains(@class,'head')], 1)"/>
</xsl:copy>
</xsl:template>
这篇关于如何将平面xml数据转换为分层数据xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文