我有一些複雜的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