【问题标题】:How to keep the order of the xml elements while transform using xslt2.0?使用 xslt2.0 进行转换时如何保持 xml 元素的顺序?
【发布时间】:2011-11-21 12:01:37
【问题描述】:

这是 XML 文档。

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

  <w:body>
    <w:p>
      <w:pPr>
        <w:pStyle w:val="Heading1"/>
      </w:pPr>
      <w:r>
        <w:t>Tables</w:t>
      </w:r>
    </w:p>

    <w:p>
      <w:r>
        <w:t>Table1</w:t>
      </w:r>
    </w:p>

    <w:tbl>
      <w:tr>
        <w:tc>
          <w:p>
            <w:r>
              <w:t>row1col1</w:t>
            </w:r>
          </w:p>
        </w:tc>
        <w:tc>
          <w:p>
            <w:r>
              <w:t>row1col2</w:t>
            </w:r>
          </w:p>
        </w:tc>
      </w:tr>

      <w:tr>
        <w:tc>
          <w:p>
            <w:r>
              <w:t>row2col1</w:t>
            </w:r>
          </w:p>
        </w:tc>

        <w:tc>
          <w:p>
            <w:r>
              <w:t>row2col2</w:t>
            </w:r>
          </w:p>
        </w:tc>
      </w:tr>
    </w:tbl>

    <w:p>
      <w:r>
        <w:t>Table2</w:t>
      </w:r>
    </w:p>

    <w:tbl>
      <w:tr>
        <w:tc>
          <w:p>
            <w:r>
              <w:t>row11col11</w:t>
            </w:r>
          </w:p>
        </w:tc>

        <w:tc>
          <w:p>
            <w:r>
              <w:t>row11col12</w:t>
            </w:r>
          </w:p>
        </w:tc>
      </w:tr>

      <w:tr>
        <w:tc>
          <w:p>
            <w:r>
              <w:t>row12col11</w:t>
            </w:r>
          </w:p>
        </w:tc>

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

我想使用我的 xslt 文件将此 xml 文档转换为下面提到的格式。

<Document>
<Heading1>
<title>Tables</title>
<paragraph>Table1</paragraph>
<table>
   <paragraph>row1col1</paragraph>
   <paragraph>row1col2</paragraph>
   <paragraph>row2col1</paragraph>
   <paragraph>row2col2</paragraph>
</table>
<paragraph>Table2</paragraph>
<table>
   <paragraph>row11col11</paragraph>
   <paragraph>row11col12</paragraph>
   <paragraph>row12col11</paragraph>
   <paragraph>row12col12</paragraph>
</table>
</Heading1>
</Document>

这是我的 XSLT 文件供您参考...

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                              xmlns:fn="http://www.w3.org/2005/xpath-functions">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="*">

    <Document>

      <xsl:variable name="headingName" select="(//w:body/w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val"/>
      <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr/w:pStyle/@w:val = $headingName]"/>

      <xsl:choose>

        <xsl:when test="$topLevelHeadings">
          <xsl:apply-templates select="$topLevelHeadings">
          </xsl:apply-templates>
        </xsl:when>

        <xsl:otherwise>
          <xsl:apply-templates select="//w:p[w:r[w:t]]">
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </Document>
  </xsl:template>


  <xsl:template match="w:p">
    <Paragraph>
      <xsl:apply-templates select="./w:r/w:t"/>
    </Paragraph>
    <xsl:apply-templates select="descendant::w:p">
    </xsl:apply-templates>
  </xsl:template>


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



  <xsl:template match="//w:body/w:p[w:pPr[w:pStyle[starts-with(@w:val,'Heading')]]]">

    <Heading1>
      <Title>
        <xsl:apply-templates select="./w:r/w:t"/>
      </Title>

      <xsl:choose>

        <xsl:when test="following-sibling::w:tbl//w:p[w:r[w:t]]">
          <xsl:for-each select="following-sibling::w:tbl">

            <table>
              <xsl:apply-templates select="descendant::w:p ">
              </xsl:apply-templates>
            </table>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="descendant::w:p">
            <!-- | following-sibling::w:tbl//w:p[w:r[w:t]]-->
          </xsl:apply-templates>
        </xsl:otherwise>

      </xsl:choose>

      <xsl:apply-templates select="following-sibling::w:p[w:r and not(w:pPr[w:pStyle])] | following-sibling::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
      </xsl:apply-templates>

      <xsl:variable name="nextHead" select="concat('Heading', number(substring-after('Heading1', 'Heading'))+1)"/>

      <!-- Get a list of child nodes (headings) for the current node -->
      <xsl:variable name="nextLevelHeadings" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$nextHead]]]"/>

      <!-- Apply recursively for next level headings within the scope -->
      <xsl:apply-templates select="$nextLevelHeadings">
      </xsl:apply-templates>

      <!-- Close heading tag -->
    </Heading1>

  </xsl:template>
</xsl:stylesheet>

但输出是:

<Document>
<Heading1>
 <Title>Table Manipulation</Title>
  <table>
       <paragraph>row1col1</paragraph>
       <paragraph>row1col2</paragraph>
       <paragraph>row2col1</paragraph>
       <paragraph>row2col2</paragraph>
  </table>
  <table>
       <paragraph>row11col11</paragraph>
       <paragraph>row11col12</paragraph>
       <paragraph>row12col11</paragraph>
       <paragraph>row12col12</paragraph>
 </table>
  <Paragraph>Table1</Paragraph>
  <Paragraph>Table2</Paragraph>
  </Heading1>
</Document>

所以,请指导我解决这个问题,它将像我上面所说的输出要求一样工作。因为,我想在不改变段落或表格顺序的情况下转换这个 xml 文件。

感谢和问候,
p.saravanan

【问题讨论】:

  • 我已经在您提供的 XML 上尝试了样式表(在修复前缀 w 的命名空间之后),但得到的结果与您在帖子末尾显示的输出不同。您确定这就是您所提供的 XML 和 XSLT 所得到的吗?
  • @G_H:是的,它产生了我在帖子末尾提到的相同输出。
  • @Saravanan 我复制了您的 .xml 和 .xslt 并运行转换。我没有得到您声称拥有的输出。
  • @G_H:我已经修改了我的输入 xml 文档,供您参考。请参考。
  • @FailedDev:我已经修改了我的输入 xml 文档,供您参考。请参考。

标签: xml xslt xpath xslt-2.0


【解决方案1】:

通过正确缩进你的代码,它表明你错过了&lt;/w:p&gt;

我将其缩进并添加到此处。这可能会也可能不会解决您的问题。

<w:document xmlns:w="w">
    <w:body>
        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading1"/>
            </w:pPr>
            <w:r>
                <w:t>Tables</w:t>
            </w:r>
        </w:p>

        <w:p>
            <w:r>
                <w:t>Table1</w:t>
            </w:r>

            <w:tbl>
                <w:tr>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row1col</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row1co2</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
                <w:tr>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row2col1</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row2col2</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
            </w:tbl>

            <w:p>
                <w:r>
                    <w:t>Table2</w:t>
                </w:r> 
            </w:p>           

            <w:tbl>
                <w:tr>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row11col11</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row11co12</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
                <w:tr>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row12col11</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:p>
                            <w:r>
                                <w:t>row12col12</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:tr>
            </w:tbl>
        </w:p>
    <w:body>
</w:document>

【讨论】:

  • 最好使用评论或建议编辑。无论如何,它不会解决问题,可能是一个错字或从原始文档中剪掉太多的结果。
  • @Jan Dragsbaek:谢谢...我已经更新了我的问题。我只是在这里错过了...
  • @G_H:对不起 G_H 。为了方便参考,我已经剪掉了很多不必要的代码。如果我这样做,您将无法理解文件。这就是我放置小长度文件的原因。请理解我......
【解决方案2】:

我已经完成了这项任务,感谢大家在这方面的合作......

我刚刚附加了修改后的 xslt 文件,该文件绝对可以正常工作...

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                              xmlns:fn="http://www.w3.org/2005/xpath-functions">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="*">

    <Document>

      <xsl:variable name="headingName" select="(//w:body/w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val"/>
      <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr/w:pStyle/@w:val = $headingName]"/>

      <xsl:choose>

        <xsl:when test="$topLevelHeadings">
          <xsl:apply-templates select="$topLevelHeadings">
          </xsl:apply-templates>
        </xsl:when>

        <xsl:otherwise>
          <xsl:apply-templates select="//w:p[w:r[w:t]]">
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </Document>
  </xsl:template>


  <xsl:template match="w:p">
    <Paragraph>
      <xsl:apply-templates select="./w:r/w:t"/>
    </Paragraph>
    <xsl:apply-templates select="descendant::w:p">
    </xsl:apply-templates>
  </xsl:template>


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



  <xsl:template match="//w:body/w:p[w:pPr[w:pStyle[starts-with(@w:val,'Heading')]]]">

    <Heading1>
      <Title>
        <xsl:apply-templates select="./w:r/w:t"/>
      </Title>

     <xsl:for-each select="following-sibling::*">
        <xsl:choose>
          <xsl:when test="descendant::w:p">
            <table>
              <xsl:apply-templates select="descendant::w:p ">
              </xsl:apply-templates>
            </table>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="self::w:p[w:r and not(w:pPr[w:pStyle])] | self::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
            </xsl:apply-templates>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>

      <!--<xsl:apply-templates select="following-sibling::w:p[w:r and not(w:pPr[w:pStyle])] | following-sibling::w:p[w:r and not(w:pPr[w:pStyle[starts-with(@w:val,'Heading')]])]">
      </xsl:apply-templates>-->

      <xsl:variable name="nextHead" select="concat('Heading', number(substring-after('Heading1', 'Heading'))+1)"/>

      <!-- Get a list of child nodes (headings) for the current node -->
      <xsl:variable name="nextLevelHeadings" select="following-sibling::w:p[w:pPr[w:pStyle[@w:val=$nextHead]]]"/>

      <!-- Apply recursively for next level headings within the scope -->
      <xsl:apply-templates select="$nextLevelHeadings">
      </xsl:apply-templates>

      <!-- Close heading tag -->
    </Heading1>

  </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2010-11-28
    相关资源
    最近更新 更多