【问题标题】:How to get the particular xml element values in the same order in xml file using xslt2.0?如何使用 xslt2.0 在 xml 文件中以相同的顺序获取特定的 xml 元素值?
【发布时间】:2012-06-21 11:45:34
【问题描述】:

这是我的 XML 文件。我想使用 xslt 将此 xml 文件转换为另一个自定义的 xml 文件。

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:r>
            <w:t>Text1-</w:t>
        </w:r>
        <w:smartTag>
            <w:smartTag>
                <w:smartTag>
                    <w:smartTag>
                        <w:r>
                            <w:t>Text2-</w:t>
                        </w:r>
                    </w:smartTag>
                </w:smartTag>
                <w:r>
                    <w:t>Text3-</w:t>
                </w:r>
                <w:smartTag>
                        <w:r>
                            <w:t>Text4-</w:t>
                        </w:r>
                </w:smartTag>
                <w:r>
                    <w:t>Text5-</w:t>
                </w:r>
            </w:smartTag>
        </w:smartTag>
        <w:r>
            <w:t>Text6-</w:t>
        </w:r>
    </w:p>
    </w:body>
    </w: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">                              
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="*">

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


  <xsl:template match="w:p">
    <Paragraph>

     <xsl:if test="(.//w:smartTag/w:r/w:t)">
            <xsl:apply-templates select="//w:smartTag//w:r//w:t"/>
     </xsl:if>
    <xsl:apply-templates select="./w:r/w:t"/>
    </Paragraph>    
  </xsl:template>


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

我当前的输出是:

<Document>
<Paragraph>
       Text2-Text3-Text4-Text5-Text1-Text6-
</Paragraph>
</Document>

我需要的输出是:

<Document>
    <Paragraph>
           Text1-Text2-Text3-Text4-Text5-Text6-
    </Paragraph>
</Document>

请指导我在不丢失其保留顺序的情况下获取元素...

【问题讨论】:

    标签: xml xslt xpath xslt-2.0


    【解决方案1】:

    除非你有一些关于应该处理什么的额外规则,否则这可以通过一个模板来匹配 w:t 元素

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

    您还需要匹配来处理文档和段落。试试下面的 XML

    <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="/">
          <Document>
             <xsl:apply-templates />
          </Document>
       </xsl:template>
    
       <xsl:template match="w:p">
          <Paragraph>
             <xsl:apply-templates />
          </Paragraph>
       </xsl:template>
    
       <xsl:template match="w:r/w:t">
          <xsl:value-of select="." />
       </xsl:template>
    
       <!-- Ignore text for all other elements -->
       <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    当应用于您的示例 XML 时,将输出以下内容

    <Document>
       <Paragraph>Text1-Text2-Text3-Text4-Text5-Text6-</Paragraph>
    </Document>
    

    【讨论】:

    • 我同意采用的方法,但对于许多 XSLT 处理器(分别使用某些 XML 解析器的 XSLT 处理器),我认为您需要添加 &lt;xsl:strip-space elements="*"/&gt;&lt;xsl:template match="text()"/&gt; 以防止 XML 输入中的空白被复制到输出。
    • 好点。我添加了一个&lt;xsl:template match="text()"/&gt; 模板,以便忽略其他文本。
    • @Tim:非常感谢... TIM
    【解决方案2】:

    简短:

    <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 omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/*/*/w:p">
         <Document>
          <Paragraph>
            <xsl:value-of select="string()"/>
          </Paragraph>
         </Document>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于提供的 XML 文档时:

    <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
            <w:p>
                <w:r>
                    <w:t>Text1-</w:t>
                </w:r>
                <w:smartTag>
                    <w:smartTag>
                        <w:smartTag>
                            <w:smartTag>
                                <w:r>
                                    <w:t>Text2-</w:t>
                                </w:r>
                            </w:smartTag>
                        </w:smartTag>
                        <w:r>
                            <w:t>Text3-</w:t>
                        </w:r>
                        <w:smartTag>
                            <w:r>
                                <w:t>Text4-</w:t>
                            </w:r>
                        </w:smartTag>
                        <w:r>
                            <w:t>Text5-</w:t>
                        </w:r>
                    </w:smartTag>
                </w:smartTag>
                <w:r>
                    <w:t>Text6-</w:t>
                </w:r>
            </w:p>
        </w:body>
    </w:document>
    

    产生了想要的正确结果:

    <Document>
       <Paragraph>Text1-Text2-Text3-Text4-Text5-Text6-</Paragraph>
    </Document>
    

    【讨论】:

    • @Saravanan:不客气。请随时接受真正最好的答案:)
    • @DimitreNovaatchev:显然你没有注意到,他已经注意到了。
    猜你喜欢
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2023-04-01
    • 2014-07-28
    • 2019-12-28
    相关资源
    最近更新 更多