简体   繁体   English

如何使用xslt2.0在xml文件中以相同的顺序获取特定的xml元素值?

[英]How to get the particular xml element values in the same order in xml file using xslt2.0?

This is my Xml file.i want to transform this xml file into another customized xml file by using xslt. 这是我的Xml文件。我想通过使用xslt将此xml文件转换为另一个自定义的xml文件。

XML FILE: 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>

and MY XSLT Snippt is : 和我的XSLT Snippt是:

<?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>

My Current Output is : 我目前的输出是:

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

My Required Output is : 我的要求输出是:

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

Please Guide me to get the elements without losing the order it preserves... 请指导我获取元素而不会丢失它保留的顺序......

Unless you have some extra rules about what should be processed, this could done quite simply by having a template to match w:t elements 除非你有一些关于应该处理什么的额外规则,否则这可以通过使模板匹配w:t元素来完成

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

You would also need matchs to handle the document and paragraph. 您还需要匹配来处理文档和段落。 Try the following XML 请尝试以下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>

When applied to your sample XML, the following is output 应用于示例XML时,将输出以下内容

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

Short and simple: 简短而简单:

<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>

When this transformation is applied on the provided XML document: 在提供的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>

the wanted, correct result is produced: 产生了想要的正确结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM