我想将所有文本放在XML文件中W:pPr标记的heading2属性之后的div HTML标记中,直到出现下一个heading2属性。这是XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type = "text/xsl" href = "doc.xsl"?>
<w:document
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14">
<w:body>
<w:p w:rsidR="00885875" w:rsidRDefault="001E1A54" w:rsidP="001E1A54">
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>My First Course</w:t>
</w:r>
</w:p>
<w:p w:rsidR="001E1A54" w:rsidRDefault="001E1A54" w:rsidP="001E1A54">
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r>
<w:t>My First Slide</w:t>
</w:r>
</w:p>
<w:p w:rsidR="001E1A54" w:rsidRDefault="001E1A54" w:rsidP="008B5F33">
<w:pPr>
<w:pStyle w:val="CourseParagraph"/>
</w:pPr>
<w:r w:rsidRPr="001E1A54">
<w:t>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="008B5F33" w:rsidRDefault="008B5F33" w:rsidP="008B5F33">
<w:pPr>
<w:pStyle w:val="CourseParagraph"/>
</w:pPr>
</w:p>
<w:p w:rsidR="008B5F33" w:rsidRDefault="008B5F33" w:rsidP="008B5F33">
<w:pPr>
<w:pStyle w:val="Heading2"/>
</w:pPr>
<w:r>
<w:t>My Second Slide</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
<w:p w:rsidR="001E1A54" w:rsidRPr="001E1A54" w:rsidRDefault="001E1A54" w:rsidP="001E1A54">
<w:pPr>
<w:pStyle w:val="CourseParagraph"/>
</w:pPr>
<w:r w:rsidRPr="001E1A54">
<w:t>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</w:t>
</w:r>
</w:p>
<w:p w:rsidR="001E1A54" w:rsidRPr="001E1A54" w:rsidRDefault="001E1A54" w:rsidP="001E1A54">
<w:pPr>
<w:pStyle w:val="CourseParagraph"/>
</w:pPr>
<w:r w:rsidRPr="001E1A54">
<w:t>this is copied</w:t>
</w:r>
</w:p>
<w:sectPr w:rsidR="001E1A54" w:rsidRPr="001E1A54">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
HTML格式应类似于
<div>
<h2>Text of w:p tag where attribute value of w:pStyle is Heading2<h2>
<p> Text of w:p tag where attribute value of w:pStyle is CourseParagraph/>
<div>
上面的div将包含所有文本,直到下一个标题2出现,并且遇到下一个标题2时将形成一个新的div。
我正在使用此代码。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="//w:body">
<xsl:for-each select="w:p">
<xsl:if test="w:pPr/w:pStyle[@w:val='Heading2']">
<div>
<h2>
<xsl:value-of select="w:r/w:t"/>
</h2>
<xsl:for-each select="following::w:p">
<xsl:choose>
<xsl:when test="w:pPr/w:pStyle[@w:val='CourseParagraph']">
<p>
<xsl:value-of select="w:r/w:t"/>
</p>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</div>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在输出中,出现标题2之后的所有段落值。在下一个标题2之前,我无法进行限制。
答案 0 :(得分:0)
例如,使用for-each-group
<xsl:for-each-group select="w:p" group-starting-with="w:p[w:pPr/w:pStyle[@w:val='Heading2']]">
<div>
<xsl:apply-templates select="current-group()"/>
</div>
</xsl:for-each-group>
然后编写模板
<xsl:template match="w:p">
<p>
<xsl:apply-templates select="w:r/w:t"/>
</p>
</xsl:template>
<xsl:template match="w:p[w:pPr/w:pStyle[@w:val='Heading2']]">
<h2>
<xsl:apply-templates select="w:r/w:t"/>
</h2>
</xsl:template>