我有一些复杂的MS-Office XML,看起来像您在链接中看到的内容,但是完整的源代码更长,文档根的许多p:sld
和p:notes
子级。总是出现在顺序p:sld
,p:notes
,p:sld
,p:notes
http://pastie.org/9604783为特定祖先的每个实例选择某个特定名称的第一个后代
感谢JLRishe,我有一些XSL提取后代a:t
元素和基于上下文的各种标签的包装及其内容。
这XSL是如下
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
<xsl:output method="xml"/>
<xsl:template match="/">
<document>
<xsl:apply-templates select="//a:t"/>
</document>
</xsl:template>
<xsl:template match="a:t">
<xsl:variable name="sldAncestor" select="ancestor::p:sld" />
<xsl:variable name="notesAncestor" select="ancestor::p:notes" />
<xsl:variable name="rAncestorPreLevel"
select="ancestor::a:r/preceding-sibling::*[1]/@lvl" />
<xsl:variable name="wrapperName">
<xsl:choose>
<xsl:when test="$sldAncestor and $rAncestorPreLevel = '1'">
<xsl:text>SlideBullet</xsl:text>
</xsl:when>
<xsl:when test="$sldAncestor and $rAncestorPreLevel = '2'">
<xsl:text>SlideBullet1</xsl:text>
</xsl:when>
<xsl:when test="$sldAncestor and $rAncestorPreLevel = '3'">
<xsl:text>SlideBullet2</xsl:text>
</xsl:when>
<xsl:when test="$notesAncestor and $rAncestorPreLevel = '0'" >
<xsl:text>StudentNotes</xsl:text>
</xsl:when>
<xsl:when test="$notesAncestor and $rAncestorPreLevel = '1'" >
<xsl:text>StudentNotes</xsl:text>
</xsl:when>
<xsl:when test="$notesAncestor and $rAncestorPreLevel = '2'">
<xsl:text>Student_Notes_Bullet</xsl:text>
</xsl:when>
<xsl:when test="$notesAncestor and $rAncestorPreLevel = '3'">
<xsl:text>Student_Notes_Bullet_1</xsl:text>
</xsl:when>
<xsl:otherwise>Body</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$wrapperName}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但我想展开能够选择每个p:sld
内出现的第一个a:t
元素和包装,在标签<SlideTitleGhost></SlideTitleGhost>
。
同样地,我希望能够选择每个p.notes
元素 中的第一个a:t
元素,并与标签<PageBreak /><StudentNotes></StudentNotes>
请注意,并非所有的a:t
元素是同级的包裹的内容。同胞a:t
元素是a:r
元素的子元素,但是有多个a:r
元素来自各个p:notes
或p:sld
元素。那些a:r
元素也不能期望是兄弟姐妹。每个a:t
元素的xpath的最后一部分变为//p:cSld/p:spTree/p:sp/p:txBody/a:p/a:r/a:t
我在Windows上使用Saxon-HE,但如果需要可以切换处理器。
期望的输出将如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
<SlideTitleGhost>header text</SlideTitleGhost>
<Body>body text </Body>
<Body>body text </Body>
<Body>body text </Body>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet1>bulleted2 text</SlideBullet1>
<SlideBullet1>bulleted2 text</SlideBullet1>
<SlideBullet1>bulleted2 text</SlideBullet1>
<SlideBullet1>bulleted2 text</SlideBullet1>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet>bulleted text</SlideBullet>
<SlideBullet>bulleted text</SlideBullet>
<Body>body text</Body>
<Body>body text</Body>
<Body>footer text</Body>
<Body>10</Body>
<Body>10</Body>
<PageBreak />
<StudentNotes>notes header text</StudentNotes>
<Body>notes body text</Body>
<StudentNotes>notes body text</StudentNotes>
<StudentNotes>notes table header text</StudentNotes>
<StudentNotes>notes table header text</StudentNotes>
<StudentNotes>notes table body text</StudentNotes>
<StudentNotes>table body text</StudentNotes>
<StudentNotes>notes table body text</StudentNotes>
<StudentNotes>notes table body text</StudentNotes>
<StudentNotes>notes table body text</StudentNotes>
<StudentNotes>notes table body text</StudentNotes>
</document>
为每个案例设置模板,调整模板优先级,以便在默认情况下异常匹配。 – keshlam 2014-10-01 04:08:09