2011-09-21 54 views
1

這是我的xml文件...如何使用xslt 2.0在w:body/w:p中獲取所有w:r/w:t的查詢?

<w:document> 

     <w:body> 

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

這是我的第二個XML文件...

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

在這裏,我已經寫了一個XSLT文件,並要求我的模板,每當我發現//瓦特:主體/ w的:p/W:R/W:噸。

for example, 


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

我自己的模板是

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

我的XSLT與我的第一個XML document.But它正常工作,不與第二個,也有一些場景像that.So工作,我怎樣才能達到既這種情況下通過修改此查詢...的

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

請指引我走出這個問題...

回答

1

使用

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

您可以ASO更改模板的match屬性稍微更具體:

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

完整代碼

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

當這一轉變應用於第一個提供的XML文檔(命名空間中定義,使其充分形成的):

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

正確的結果產生

   Paragraph1 

當相同的變換被應用在第二提供 「XML」(它是嚴重畸形,我花了很多分鐘,直到我把它做好了):

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

再次通緝的結果產生

    Paragraph2 
+0

:對不起Dimitre,這不符合我上述情況的工作......我認爲他並沒有叫我的模板... – Saravanan

+0

@Saravan:如果有*任何匹配匹配模式的*元素,那麼必須選擇模板執行 - 否則,可能有另一個模板也匹配此元素並具有更高的優先級/優先級。 –

+0

@現在我只用我的第二個XML文件進行了測試,我發現沒有匹配的元素... – Saravanan

相關問題