我有一个h1节标题及其相应的段落(段落,列表)和大量的h2节标题及其对应的段落(段落,列表)。我想将每个h1和h2更改为具有不同ID的主题
XML输入为:
<?xml version="1.0"?>
<ImportArticle>
<Segments>
<Segment>
<Body>
<h1>Essay on Education</h1>
<h2>Importance of Education Essay</h2>
<p>Following are different types of essay topics</p>
<h2>Essay on Music</h2>
<p>Education is the systematic process of improving learning</p>
<ul>
<li>
<p>
<b>Find understandable essay.</b> Education essay is the most important</p>
</li>
<li>
<p>
<b>Education is the act.</b> It helps us to easily understand and deal with any problem.</p>
</li>
</ul>
<p>It improves our knowledge, skill, confidence level and personality.</p>
</Body>
</Segment>
</Segments>
</ImportArticle>
XSL我用作:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
indent="yes"
omit-xml-declaration="no"
doctype-public="urn:pubid:com.doctype.doctypes:doctypes:dita:topic"
doctype-system="topic.dtd"/>
<xsl:strip-space elements="*"/>
<xsl:template match="ImportArticle">
<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="dita.xsl"</xsl:processing-instruction>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Segments">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Segment">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="h1">
<topic id="topic_1">
<title>
<xsl:apply-templates/>
</title>
</topic>
</xsl:template>
<xsl:template match="h2">
<topic id="topic_2">
<title>
<xsl:apply-templates/>
</title>
</topic>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="ul">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="li">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<xsl:template match="b">
<b>
<xsl:apply-templates/>
</b>
</xsl:template>
</xsl:stylesheet>
输出我得到:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="DITA.xsl"?>
<!DOCTYPE topic
PUBLIC "urn:pubid:com.doctypes.doctypes:doctypes:dita:topic" "topic.dtd">
<topic id="topic_1">
<title>Essay on Education</title>
</topic>
<topic id="topic_2">
<title>Importance of Education Essay</title>
</topic>
<p>Following are different types of essay topics</p>
<topic id="topic_2">
<title>Essay on Music</title>
</topic>
<p>Education is the systematic process of improving learning</p>
<ul>
<li><p><b>Find understandable essay.</b> Education essay is the most important</p></li>
<li><p><b>Education is the act.</b> It helps us to easily understand and deal with any problem.</p></li>
</ul>
<p>It improves our knowledge, skill, confidence level and personality.</p>
但我期待输出为&#34; h1&#34;主题需要在所有&#34; h2&#34;之后关闭关闭,每个&#34; h2&#34;需要有不同的id和para和list来源于输入的地方:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="DITA.xsl"?>
<!DOCTYPE topic
PUBLIC "urn:pubid:com.doctypes.doctypes:doctypes:dita:topic" "topic.dtd">
<topic id="topic_1">
<title>Essay on Education</title>
<topic id="topic_2">
<title>Importance of Education Essay</title>
<p>Following are different types of essay topics</p>
</topic>
<topic id="topic_3">
<title>Essay on Music</title>
<p>Education is the systematic process of improving learning</p>
<ul>
<li><p><b>Find understandable essay.</b> Education essay is the most important</p></li>
<li><p><b>Education is the act.</b> It helps us to easily understand and deal with any problem.</p></li>
</ul>
<p>It improves our knowledge, skill, confidence level and personality.</p>
</topic>
</topic>
请就此提出建议并提前致谢
答案 0 :(得分:2)
如果您按照指示使用XSLT 2.0,则可以使用for-each-group starting-with="h1"
:
<xsl:output
method="xml"
indent="yes"
omit-xml-declaration="no"
doctype-public="urn:pubid:com.doctype.doctypes:doctypes:dita:topic"
doctype-system="topic.dtd"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="dita.xsl"</xsl:processing-instruction>
<xsl:apply-templates select="//Body"/>
</xsl:template>
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:apply-templates select="current-group() except ."
/> </topic>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
</xsl:transform>