如何根据xslt中的"Heading"以其属性值开始选择xml元素?

Sar*_*nan 8 xml xslt xpath

每当我发现xml元素的匹配时,我想调用我自己的xsl模板,其属性值以"Heading"开头.如何在Xslt中进行此查询.

例如:

<w:p>
  <w:pPr>
     <w:pStyle w:val="Heading2"/>
  </w:pPr>
</w:p>

<w:p>
  <w:pPr>
     <w:pStyle w:val="Heading1"/>
  </w:pPr>
</w:p>

<w:p>
  <w:pPr>
     <w:pStyle w:val="Heading2"/>
  </w:pPr>
</w:p>

<w:p>
  <w:pPr>
     <w:pStyle w:val="ListParagraph"/>
  </w:pPr>
</w:p>

<w:p>
  <w:pPr>
     <w:pStyle w:val="commentText"/>
  </w:pPr>
</w:p>
Run Code Online (Sandbox Code Playgroud)

所以,我想查询w:pStyle - > w:val仅以"标题"开头.

请帮我解决这个问题......

Tim*_*m C 13

您可以通过使用XPath字符串函数来实现此目的

<xsl:template match="w:pStyle[starts-with(@w:val, 'Heading')]">
Run Code Online (Sandbox Code Playgroud)

这简单地匹配所有w:pStyle节点,其中w:val属性以单词Heading开头.然后,您可以将自己的代码放在此模板中.

以下是如何在XSLT标识转换中使用它的示例

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://mynamespace.com">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="w:pStyle[starts-with(@w:val, 'Heading')]">
      <!-- Your code here -->
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

上面的XSLT,除非你在它所说的地方添加了你自己的代码,否则将从XML中删除所有数学w:pStyle元素.