How to select an xml element based on its attribute value to start with β€œTitle” in xslt?

I want to call my own xsl template whenever I find the xml element matches its attribute value, starting with "Title". How to make this request in Xslt.

eg:

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

So, I want to make a request that w: pStyle β†’ w: val, starting with "Title".

Please help me out of this problem ...

+8
xml xpath xslt
source share
1 answer

You can achieve this using the XPath string function, starting with

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

This simply matches all w: pStyle nodes , where the w: val attributes begin with the word Heading . Then you can place your own code in this template.

Here is an example of how you will use it in XSLT identity transformation

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

The aforementioned XSLT, unless you have added your own code where it says, will strip all the mathcing w: pStyle elements from XML.

+13
source share

All Articles