如何根据xslt中的“标题”以属性值为基础选择xml元素?

前端之家收集整理的这篇文章主要介绍了如何根据xslt中的“标题”以属性值为基础选择xml元素?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每当我发现xml元素的匹配时,我想调用我自己的xsl模板,其属性值以“Heading”开头.如何在Xslt中进行此查询.

例如:

  1. <w:p>
  2. <w:pPr>
  3. <w:pStyle w:val="Heading2"/>
  4. </w:pPr>
  5. </w:p>
  6.  
  7. <w:p>
  8. <w:pPr>
  9. <w:pStyle w:val="Heading1"/>
  10. </w:pPr>
  11. </w:p>
  12.  
  13. <w:p>
  14. <w:pPr>
  15. <w:pStyle w:val="Heading2"/>
  16. </w:pPr>
  17. </w:p>
  18.  
  19. <w:p>
  20. <w:pPr>
  21. <w:pStyle w:val="ListParagraph"/>
  22. </w:pPr>
  23. </w:p>
  24.  
  25. <w:p>
  26. <w:pPr>
  27. <w:pStyle w:val="commentText"/>
  28. </w:pPr>
  29. </w:p>

所以,我想查询w:pStyle – > w:val仅以“标题”开头.

请帮我解决这个问题……

您可以通过使用XPath字符串函数来实现此目的
  1. <xsl:template match="w:pStyle[starts-with(@w:val,'Heading')]">

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

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

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://mynamespace.com">
  2. <xsl:output method="xml" indent="yes"/>
  3.  
  4. <xsl:template match="w:pStyle[starts-with(@w:val,'Heading')]">
  5. <!-- Your code here -->
  6. </xsl:template>
  7.  
  8. <xsl:template match="@*|node()">
  9. <xsl:copy>
  10. <xsl:apply-templates select="@*|node()"/>
  11. </xsl:copy>
  12. </xsl:template>
  13. </xsl:stylesheet>

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

猜你在找的XML相关文章