我有四个名为h1,h2,h3和h4的标题,我希望每个标题都需要根据DTD的不同风格
XML输入是:
<Segments>
<Segment>
<Body>
<h1>Introduction:</h1>
<h2>Introductory Paragraph</h2>
<p>The introductory paragraph should also include the thesis statement</p>
<h3>Body — First paragraph:</h3>
<p>The first paragraph of the body should contain the strongest argument</p>
<h4>Conclusion:</h4>
<p>an allusion to the pattern used in the introductory paragraph</p>
<h4>Concluding paragraph:</h4>
<p>On the days you get treatment</p>
</Body>
</Segment>
</Segments>
XSL我用作:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all">
<xsl:output
method="xml"
indent="yes"
omit-xml-declaration="no"
doctype-public="urn:pubid:com.doctypes.doctypes:doctypes:dita:topic"
doctype-system="topic.dtd"/>
<xsl:strip-space elements="*"/>
<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="Segments">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Segment">
<xsl:apply-templates/>
</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 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</subsection>
</xsl:for-each-group>
</subsection>
</xsl:for-each-group>
</subsection>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</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>Introduction:</title>
<subsection id="topic_2">
<title>Introductory Paragraph</title>
<body>
<p>The introductory paragraph should also include the thesis statement</p>
<h3>Body — First paragraph:</h3>
<p>The first paragraph of the body should contain the strongest argument</p>
<h4>Conclusion:</h4>
<p>an allusion to the pattern used in the introductory paragraph</p>
<h4>Concluding paragraph:</h4>
<p>On the days you get treatment</p>
</body>
<subsection id="topic_">
<title>The introductory paragraph should also include the thesis statement</title>
<body/>
</subsection>
<subsection id="topic_3">
<title>Body — First paragraph:</title>
<body>
<p>The first paragraph of the body should contain the strongest argument</p>
<h4>Conclusion:</h4>
<p>an allusion to the pattern used in the introductory paragraph</p>
<h4>Concluding paragraph:</h4>
<p>On the days you get treatment</p>
</body>
<subsection id="topic_">
<title>The first paragraph of the body should contain the strongest argument</title>
<body/>
</subsection>
<subsection id="topic_4">
<title>Conclusion:</title>
<body>
<p>an allusion to the pattern used in the introductory paragraph</p>
</body>
</subsection>
<subsection id="topic_5">
<title>Concluding paragraph:</title>
<body>
<p>On the days you get treatment</p>
</body>
</subsection>
</subsection>
</subsection>
</topic>
输出我想要:
<?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>Introduction:</title>
<subsection id="topic_2">
<title>Introductory Paragraph</title>
<body>
<p>The introductory paragraph should also include the thesis statement</p>
</body>
<subsection id="topic_3">
<title>Body — First paragraph:</title>
<body>
<p>The first paragraph of the body should contain the strongest argument</p>
</body>
<subsection id="topic_4">
<title>Conclusion:</title>
<body>
<p>an allusion to the pattern used in the introductory paragraph</p>
</body>
</subsection>
<subsection id="topic_5">
<title>Concluding paragraph:</title>
<body>
<p>On the days you get treatment</p>
</body>
</subsection>
</subsection>
</subsection>
</topic>
请指导我。提前致谢
答案 0 :(得分:0)
您可以重写for-each-group
用途,以检查第一项是否为标题:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all">
<xsl:output
method="xml"
indent="yes"
omit-xml-declaration="no"
doctype-public="urn:pubid:com.doctypes.doctypes:doctypes:dita:topic"
doctype-system="topic.dtd"/>
<xsl:strip-space elements="*"/>
<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 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<subsection>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</subsection>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</subsection>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</subsection>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>