如何在我的函数中检查follow-sibling是w:tbl并使用xslt2.0应用我的模板?

时间:2012-07-27 07:02:13

标签: xml xpath xslt-1.0 xslt-2.0 xpath-2.0

这是我的XML文档。我想将这个XML文档转换为另一个XML文档(请参阅所需的输出XML部分)。我编写了一个函数来收集所有w:p元素以供进一步处理。现在,我有了处理{{1所以,我已经为流程w:tbl元素编写了一个模板。所以,我必须检查任何w:tbl元素之间的w:tbl元素。如果w:tbl在任何{{}之间找到1}}然后我想打电话给我的模板。

w:p

我的Xslt2.0代码是:

w:p

我的要求输出是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                xmlns:v="urn:schemas-microsoft-com:vml">
    <w:body>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading1" /> 
        </w:pPr>
        <w:r>
            <w:t>Text2-</w:t>
        </w:r>  
    </w:p>
     <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text1-</w:t>
        </w:r>  
    </w:p>
<w:tbl>            
        <w:tr>
            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>Sachin</w:t> 
                    </w:r>
                </w:p>
            </w:tc>

            <w:tc>
                <w:p>
                    <w:r>
                        <w:t>Raghul</w:t> 
                    </w:r>
                </w:p>
            </w:tc> 
        </w:tr>                 
    </w:tbl>
</w:body>
</w:document>

但我的生成输出是:

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:mf="http://example.com/mf"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
  xmlns:user="http://http://stackoverflow.com/questions/11356668"
  exclude-result-prefixes="xs w fn mf user">

  <xsl:output indent="yes"/> 

  <xsl:function name="mf:group" as="element()*">
    <xsl:param name="paragraphs" as="element()*"/>
    <xsl:param name="level" as="xs:integer"/>


<xsl:for-each-group select="$paragraphs" group-starting-with="p[pPr/pStyle/@w:val = concat('Heading', $level)]">   

  <xsl:choose>

    <xsl:when test="self::p[pPr/pStyle/@w:val = concat('Heading', $level)]">
      <xsl:element name="Heading{$level}">
        <Title>
          <xsl:apply-templates select="./r/t"/>
        </Title>                

        <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
      </xsl:element>
    </xsl:when>  
    <xsl:when test="current-group()[self::p[pPr/pStyle/@w:val = concat('Heading', $level + 1)]]">
      <xsl:sequence select="mf:group(current-group(), $level + 1)">
      </xsl:sequence>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="current-group()">
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>

 <xsl:if test="following-sibling::w:tbl">
        <xsl:apply-templates select="self::w:tbl"></xsl:apply-templates>
  </xsl:if>    
</xsl:for-each-group>
 </xsl:function>

  <xsl:template match="document">

    <Document>
      <xsl:sequence select="mf:group(body/p, 1)"/>
    </Document>

  </xsl:template>

   <xsl:template match="w:tbl">
    <table>
      <xsl:apply-templates select ="descendant::w:p[w:r/w:t]"></xsl:apply-templates>
    </table>
  </xsl:template>

 <xsl:template match="p">
  <Paragraph>
    <xsl:value-of select="r/t"/>
  </Paragraph>
</xsl:template>

  <xsl:template match="/r/t">
    <xsl:value-of select="."/>
  </xsl:template> 
</xsl:stylesheet>

请指导我摆脱这个问题...

1 个答案:

答案 0 :(得分:0)

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xpath-default-namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:mf="http://example.com/mf"
  xmlns:v="urn:schemas-microsoft-com:vml"
  xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
  xmlns:user="http://http://stackoverflow.com/questions/11356668"
  exclude-result-prefixes="xs w fn mf user">

  <xsl:output indent="yes"/> 

  <xsl:function name="mf:group" as="element()*">
    <xsl:param name="paragraphs" as="element()*"/>
    <xsl:param name="level" as="xs:integer"/>


<xsl:for-each-group select="$paragraphs" group-starting-with="p[pPr/pStyle/@w:val = concat('Heading', $level)]">   

  <xsl:choose>

    <xsl:when test="self::p[pPr/pStyle/@w:val = concat('Heading', $level)]">
      <xsl:element name="Heading{$level}">
        <Title>
          <xsl:apply-templates select="./r/t"/>
        </Title>                

        <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
      </xsl:element>
    </xsl:when>  
    <xsl:when test="current-group()[self::p[pPr/pStyle/@w:val = concat('Heading', $level + 1)]]">
      <xsl:sequence select="mf:group(current-group(), $level + 1)">
      </xsl:sequence>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="current-group()">
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>

 <xsl:if test="following-sibling::w:tbl">
        <xsl:apply-templates select="following-sibling::w:tbl"></xsl:apply-templates>
  </xsl:if>    
</xsl:for-each-group>
 </xsl:function>

  <xsl:template match="document">

    <Document>
      <xsl:sequence select="mf:group(body/p, 1)"/>
    </Document>

  </xsl:template>

   <xsl:template match="w:tbl">
    <table>
      <xsl:apply-templates select ="descendant::w:p[w:r/w:t]"></xsl:apply-templates>
    </table>
  </xsl:template>

 <xsl:template match="p">
  <Paragraph>
    <xsl:value-of select="r/t"/>
  </Paragraph>
</xsl:template>

  <xsl:template match="/r/t">
    <xsl:value-of select="."/>
  </xsl:template> 
</xsl:stylesheet>