xslt - Use XPath to find nodes whose attribute is missing from another part of XML document -
i've got xml document similar this:
<pipeline> <log> <error stagename="one" message="ack!" /> <error stagename="two" message="ack!" /> <error stagename="three" message="ack!" /> </log> <stages> <stage name="one" /> <stage name="two" /> </stages> </pipeline> i'm trying use xslt make report of errors grouped stage. @ end of report, want errors not-associated stage. i'm able report on each stage's errors. need way conditionally show other errors.
this closest i've come:
<xsl:template match="/pipeline"> <xsl:variable name="unknown.stages" select="log/error[@stagename != stages/stage/@name]" /> <xsl:if test="count($unknown.stages) > 0"> <h1>other errors</h1> <xsl:apply-templates select="$unknown.stages" /> </xsl:if> </xsl:template> but ends showing errors instead of errors not associated stage. there xpath query can use in xsl:variable declaration select error nodes stagename attribute has value doesn't exist in list of stages/stage elements?
update: here correct xpath query. have create key on stages element, , use key find errors don't match:
<xsl:key name="stagename" match="/pipeline/stages/stage" use="@name" /> <xsl:template match="/pipeline"> <xsl:variable name="unknown.stages" select="log/error[not(key('stagename',@stagename)]" /> <xsl:if test="count($unknown.stages) > 0"> <h1>other errors</h1> <xsl:apply-templates select="$unknown.stages" /> </xsl:if> </xsl:template>
are sure getting errors shown? current xpath looking stages element child of current error element selecting. have expected expression more this
<xsl:variable name="unknown.stages" select="log/error[@stagename != //stages/stage/@name]" /> having said that, returns error elements because return error elements there exists stage different name.
anyway, solve this, consider using key stage elements
<xsl:key name="stages" match="stage" use="@name" /> then can write expression this, should check existence require:
<xsl:variable name="unknown.stages"select="log/error[not(key('stages', @stagename))]" />
Comments
Post a Comment