Remove certain nodes from an xml using xslt3.0

女生的网名这么多〃 提交于 2021-01-07 02:33:00

问题


Currently i am trying to remove certain nodes from an xml using xslt3.0 using identity template. But this is taking long processing time. need some suggestion to improve performace using Xslt30Transformer.

<xsl:stylesheet version="3.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
 <xsl:output method="xml" indent="yes"/>

 <xsl:variable name="pathexcluded" select="'CATALOG/CD1 | CATALOG/CD2 '"/>
 <xsl:variable name="changed-nodes" as="node()*" >
  <xsl:evaluate xpath="$pathexcluded" context-item="/"/>
 </xsl:variable>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 
 <xsl:template match="$changed-nodes">
</xsl:template>
</xsl:stylesheet>

回答1:


As in previous example, an alternative is to use a static parameter and a shadow attribute:

<xsl:param name="pathexcluded" as="xs:string" static="yes" select="'CATALOG/CD1 | CATALOG/CD2 '"/>

<xsl:template _match="{$pathexcluded}"/>

You will have to provide details on your performance problems to allow us to judge whether xsl:evaluate is the culprit. The above is mainly posted as an alternative approach, test yourself whether it performs better with your use cases. Your example with the simple xsl:variable doesn't even make it clear whether that part is variable or not any time you run the transformation.

BTW: in XSLT 3 you can declare <xsl:mode on-no-match="shallow-copy"/> instead of setting up the identity template.




回答2:


A performance question really only makes sense if you can quantify it: what is the size of the source document, how long is it taking, how does this compare with your performance requirement?

I can't see why you're using xsl:evaluate here, given that the path expression is fixed. Perhaps it isn't fixed in your real code? But then, if this isn't your real code, perhaps you haven't shown us the actual source of the problem? Certainly if it's a large source document and you're only evaluating the xsl:evaluate once, then it's unlikely to be the cause of the trouble.

The <xsl:template match="$changed-nodes"/> might well be a problem if $changed-nodes is a very large node-set. I have a feeling we did some work recently to address this: you haven't said which Saxon release you are using. But even with those improvements, testing each node to see whether it matches the criteria for deletion is going to be better than forming the set of all such nodes and then testing each node to see whether it is a member of that set.



来源:https://stackoverflow.com/questions/64977263/remove-certain-nodes-from-an-xml-using-xslt3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!