【问题标题】:How to get the attribure value of first<w:p>node which is having its attribute value starts with "Heading" and assign to a variable in xslt2.0?如何获取属性值以“标题”开头的 first<w:p> 节点的属性值并分配给 xslt 2.0 中的变量?
【发布时间】:2011-10-01 08:46:06
【问题描述】:

这是 XML 文档。

<w:document xmlns:w="w">
 <w:body>
   <w:p>
     <w:pPr>
     <w:pStyle w:val="Normal"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para1
            </w:t>
        </w:r>
     </w:p>
   <w:p>
     <w:pPr>
     <w:pStyle w:val="Heading1"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para2
            </w:t>
        </w:r>
     </w:p>
   <w:p>
     <w:pPr>
     <w:pStyle w:val="Heading2"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para3
            </w:t>
        </w:r>
     </w:p>
   <w:p>
     <w:pPr>
     <w:pStyle w:val="Heading1"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para4
            </w:t>
        </w:r>
     </w:p>
   <w:p>
     <w:pPr>
     <w:pStyle w:val="Heading2"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para5
            </w:t>
        </w:r>
     </w:p>

   <w:tbl>
         <w:tr>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para6
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para7 
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
           </w:tr>
        </w:tbl>
     <w:p>
     <w:pPr>
       <w:pStyle w:val="Heading1"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para8
            </w:t>
        </w:r>
     </w:p>
  <w:tbl>
         <w:tr>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para9
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
            <w:tc>
               <w:p>
                  <w:r>
                    <w:t>
                         Para10
                    </w:t>
                   </w:r>
                </w:p>
              </w:tc>
           </w:tr>
        </w:tbl>
    <w:p>
     <w:pPr>
      <w:pStyle w:val="Heading2"/>
     </w:pPr>
        <w:r>
           <w:t>
               Para11
            </w:t>
        </w:r>
     </w:p>
</w:body>
</w:document>

现在,

  1. 我想先搜索&lt;w:p&gt;&lt;w:pPr&gt;&lt;w:pStyle&gt;,它的w:val 属性值以“标题”开头。

  2. 找到这个之后,将该属性值(例如,第二个&lt;w:p&gt;&lt;w:pPr&gt;&lt;w:pStyle&gt;中的Heading1)分配给一个变量(例如,xslt文件中的variableName)。

  3. 将该变量(例如,xslt 文件中的 topLevelHeadings)分配到我想要的另一个变量中。

这是供您参考的 Xslt 文件...

<xsl:template match="*">
  <Document>
       <xsl:variable name="variableName" select="?"/> <!-- here i want the stuff -->
       <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr[w:pStyle(@w:val,'$variableName')]]"/>

     <xsl:choose>
         <xsl:when test="$topLevelHeadings">
              <!-- Do things here -->
         </xsl:when>
        <xsl:otherwise>
             <!-- Do things here -->
        </xsl:otherwise>
    </xsl:choose>

  </Document>
</xsl:template>

请指导我摆脱这个问题...

【问题讨论】:

  • 您的输入 XML 中甚至没有任何 &lt;w:pPr&gt; 元素。
  • @TOmalak:非常抱歉。我现在会更新...非常感谢

标签: xml xslt xpath xslt-2.0


【解决方案1】:
<xsl:variable 
  name="variableName"     
  select="(//w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val" 
/>
<xsl:variable 
  name="topLevelHeadings" 
  select="//w:p[w:pPr/w:pStyle/@w:val = $variableName]"
/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多