2012-08-28 20 views
0

这是我的xml文档。我想用xslt2.0将它转换成另一种xml格式。与xslt 2.0中的每一个碰撞?

<?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:tbl/> 
      <w:tbl/> 
     </w:body> 
     </w:document> 

这是我的xslt 2.0代码snippt。

<xsl:for-each select="following::node()[1]"> 
    <xsl:choose> 
     <xsl:when test="self::w:tbl and (parent::w:body)"> 
       <xsl:apply-templates select="self::w:tbl"/> 
     </xsl:when> 
    </xsl:choose> 
</xsl:for-each> 


<xsl:template match="w:tbl"> 
    <table> 
     table data 
    </table> 
</xsl:template> 

我生成的输出是:

<table> 
    table data 
    <table> 
     table data 
    </table> 
</table> 

但我所需的输出是:

<table> 
    table data 
</table> 
<table> 
    table data 
</table> 
+0

你真的有使用这些“前,每个”功能?这在选择“follow :: node”等时非常难看...... – FiveO

回答

2

你不说的背景下产品的点是什么在您的xsl:换每个都被执行。事实上,您不提供这些信息可能表明您尚未理解XSLT中的上下文有多重要。不知道上下文是什么,无法纠正你的代码。

如果你的代码是正确的,那么整个的for-each可以简化为

<xsl:apply-templates select="following::node()[1][self::w:tbl][parent::w:body]"/> 
2

如果您正在寻找变换宽:TBL元素是子女元素w:身体个元素,你可能只是有一个模板匹配体元素,然后将查找TBL元素

<xsl:template match="w:body"> 
    <xsl:apply-templates select="w:tbl"/> 
</xsl:template> 

匹配模板宽:TBL会像以前一样。以下是完整的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
    exclude-result-prefixes="w"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/*"> 
     <xsl:apply-templates select="w:body"/> 
    </xsl:template> 

    <xsl:template match="w:body"> 
     <xsl:apply-templates select="w:tbl"/> 
    </xsl:template> 

    <xsl:template match="w:tbl"> 
     <table> table data </table> 
    </xsl:template> 
</xsl:stylesheet> 

当适用于您的示例XML,下面是输出

<table> table data </table> 
<table> table data </table>