开发者

XPath: is it possible to combine queries

Consider the following XML:

<root>
  <steps>
    <step>1</step>
    <step>2</step>
    <step>3</step>
    <step>4</step>
  </steps>
  <stepDetails step="1">Details</stepDetails>
  <stepDetails step="2">Details</stepDetails>
  <stepDetails step="3">Details</stepDetails>
</root>

What I need to do is find all the steps that do not have corresponding stepDetails. In the above example, only the "<step>4</step>" node would be returned.

Now, I know I can do that by querying all the steps, iterating through the collection and performing another query for each iteration. I'm hoping that there is a way of doing that with just one query. Perhaps using somethi开发者_运维百科ng like SQL's IN statement and a sub-query.

Any ideas or tips would be most appreciated.

thnx, Christoph


Try this:

/root/steps/step[not(. = /root/stepDetails/@step)]


something like this?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="steps" match="//root/stepDetails" use="@step" />

  <xsl:template match="//root">
    <root>
      <steps>
      <xsl:for-each select="steps/step[not(key('steps',text()))]">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
      </xsl:for-each>
      </steps>
    </root>
  </xsl:template>



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

</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜