开发者

how to make a query for getting all w:r/w:t inside the //w:body/w:p using xslt 2.0?

开发者 https://www.devze.com 2023-04-06 04:46 出处:网络
This is my Xml File... <w:document> <w:body> <w:p> <w:r> <w:t> Paragraph1 </w:t>

This is my Xml File...

<w:document>

        <w:body>

           <w:p>
                <w:r>
                    <w:t>
                      Paragraph1
                     </w:t>
                 </w:r>
             </w:p>
          </w:body>
</w:document>

This is my Second XML file...

 <w:document>
    <w:body>
        <w:p>
            <w:r>
               <w:pict>
                    <v:shape>
                       <v:textbox>
                           <w:txbxContent>
                              <w:p>
                                 <w:r>
                                    <w:t>
                                       Paragraph2
                                     </w:t>
                                 </w:r>
                                </w:p>
                             </w:txbxContent>
                           <v:textbox>
                        </v:shape>
                     </w:pict>
                  </w:r>
                </w:p>
              </w:b开发者_JAVA百科ody>
             </w:document>

Here, i have written one xslt file and calling my template whenever i found //w:body/w:p/w:r/w:t.

   for example,


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

my own template is

<xsl:template match="w:p">
            Do something here
       </xsl:template>

my xslt working correctly with my first xml document.But it is not working with second one and also some scenario like that.So, how can i achieve both of this scenario by modifying this query here...

<xsl:apply-templates select="?????"> <!-- how to find the case that also matching my second xml file -->
</xsl:apply-templates>

Please guide me get out of this issue...


Use:

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

You may aso change the template's match attribute to be slightly more specific:

<xsl:template match="w:p[w:r/w:t]"> 
  <!-- Processing here -->
</xsl:template> 

Complete code:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="w:w">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="w:p[w:r/w:t]">
  <xsl:value-of select="w:r/w:t"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the first provided XML document (namespace defined to make it well-formed):

<w:document xmlns:w="w:w">
    <w:body>
        <w:p>
            <w:r>
                <w:t>
                 Paragraph1
             </w:t>
            </w:r>
        </w:p>
    </w:body>
</w:document>

the correct result is produced:

             Paragraph1

when the same transformation is applied on the second provided "XML" (it is severely malformed and I spent many minutes until I made it well-formed!!!):

<w:document xmlns:w="w:w">
    <w:body>
        <w:p>
            <w:r>
                <w:pict>
                    <v:shape xmlns:v="v:v">
                        <v:textbox>
                            <w:txbxContent>
                                <w:p>
                                    <w:r>
                                        <w:t>
                      Paragraph2
                                        </w:t>
                                    </w:r>
                                </w:p>
                            </w:txbxContent>
                        </v:textbox>
                    </v:shape>
                </w:pict>
            </w:r>
        </w:p>
    </w:body>
</w:document>

again the wanted result is produced:

                  Paragraph2
0

精彩评论

暂无评论...
验证码 换一张
取 消