I am refactoring an xslt for word xml to improve performance as recommended here. I am relatively new to xslt. Why are the following statements not equivalent?
Form1
<xsl:value-of select="//w:style[@w:styleId = $styleName][ancestor::pkg:part/@pkg:name='/word/styles.xml']"/>
Form2
<xsl:value-of select="/pkg:package/pkg:part[@pkg:name='/word/styles.xml']/child::w:style[@w:styleId = $styleName]" />
Note that pkg:package is the root and pkg:part are direct children.
Form1 states that get the w:style element with the attribute equaling $styleName, whose ancestors are pkg:part with attribute @pkg:name='/word/styles.xml'.
Form 2 states that get the w:style elements with the attributes equaling $styleName who are children of pkg:package/pkg:par@pkg:name='/word/styles.xml'
The actual statement I am trying to rewrite is this one:
<xsl:value-of select="//w:style[@w:styleId = $styleName][ancestor::pkg:part/@pkg:name='/word/styles.xml']/w:pPr/w:numPr/w:numId/@w:val"/>
Thank you.
Your second XPath is on the right track, but it would only match
w:stylesthat are direct children ofpkg:part(thechild::axis is redundant here), which I don’t think they are. This should work:and I think this is an improvement over the original XPath, but it still has a
//in it. My knowledge of wordprocessingML is not very extensive, but is it the case that all<w:style>s are the children of<w:styles>elements, which are all children of a<pkg:xmlData>element? If so, this should work (split across 2 lines for readability):One other possibility for improving performance is to use a key. In your XSLT, you would define a key like this:
And then you would access the style you want like this:
Key lookups are generally very efficient, so this second option may be the better one in terms of performance.