2014-12-10 6 views
0

мне нужно сравнить 2 переменные узлы с XSLT и проверьте, есть ли элемент в $ статей1 то, что отсутствует в $ Items 2:Сравнение двух nodesets с XSLT 2.0 обнаруживать отсутствующие узлам

<!-- $Items1 --> 
<Items> 
    <Item Name="1"></Item> 
    <Item Name="2"></Item> 
    <Item Name="I'm missing"></Item> 
</Items> 

<!-- $Items2 --> 
<Items> 
    <Item Name="1"></Item> 
    <Item Name="2"></Item> 
</Items> 

То, что я так далеко работает, но мне нужно, чтобы завершить процесс после того, как обмен сообщений недостающих элементов:

<xsl:template match="/"> 
    <xsl:for-each select="$Items1/Item/@Name"> 
     <xsl:choose> 
      <xsl:when xpath-default-namespace="" test="not($Items2/@Name = current())"> 
       <xsl:message terminate="no"> 
        <xsl:text>missing items </xsl:text> 
        <xsl:value-of select="current()" /> 
       </xsl:message> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:for-each> 

есть ли способ, чтобы установить флаг или что-то, где я могу проверить после цикла и завершить процесс, или писать недостающие элементов массиву и проверить, больше ли массив:

<xsl:if test="$flag='true'"> 
     <xsl:message terminate="yes"> 
      <xsl:text>Process terminated</xsl:text> 
     </xsl:message> 
</xsl:if> 

ответ

2

Я предлагаю, чтобы определить ключ

<xsl:key name="by-name" match="Items/Item" use="@Name"/> 

затем определить переменную

<xsl:variable name="not-matched" select="$Items1/Item[not(key('by-name', @Name, $Items2))]"/> 

Теперь вы можете проверить <xsl:if test="$not-matched">...</xsl:if>.

Использование xpath-default-namespace предполагает, что, возможно, ключ должен быть <xsl:key name="by-name" match="Items/Item" use="@Name" xpath-default-namespace=""/>, мне нужно будет увидеть контекст и любые объявления пространства имен, чтобы точно указать, что вам нужно.

1

Как об этом подходе:

<xsl:template match="/"> 
    <xsl:variable name="missing" select="$Items1/Item/@Name[not(. = $Items2/@Name)]" /> 
    <xsl:choose> 
     <xsl:when test="$missing"> 
      <xsl:text>missing items&#xA;</xsl:text> 
      <xsl:for-each select="$missing"> 
       <xsl:value-of select="concat(current(), '&#xA;')" /> 
      </xsl:for-each> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- Continue normal operation --> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

или альтернативно:

<xsl:template match="/"> 
    <xsl:variable name="missing" select="$Items1/Item/@Name[not(. = $Items2/@Name)]" /> 

    <xsl:if test="$missing"> 
     <xsl:text>missing items&#xA;</xsl:text> 
     <xsl:for-each select="$missing"> 
      <xsl:value-of select="concat(current(), '&#xA;')" /> 
     </xsl:for-each> 
     <xsl:message terminate="yes"> 
      <xsl:text>Process terminated</xsl:text> 
     </xsl:message> 
    </xsl:if> 

    <!-- Continue normal operation --> 

</xsl:template> 
Смежные вопросы