【问题标题】:XSLT/XPath : excluding nodes from what was selectedXSLT/XPath : 从所选内容中排除节点
【发布时间】:2012-03-27 01:36:30
【问题描述】:

我想选择满足特定条件的节点列表,在处理完这些节点后,我想选择剩余的节点。如何在 XSLT 和 XPath 中做到这一点。

下面是场景,我有这个xml

<books>
<book name="Basic XML">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
<book name="Basic XML">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>
<book name="Basic XSLT">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
<book name="Basic XSLT">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>
<book name="Basic Java">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
<book name="Basic Java">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>
<book name="Web Service">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
<book name="C Programming">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
</books>

1。选择“教程”的&lt;type&gt;的所有&lt;book&gt;节点,下面是输出

<books>
<book name="Basic XML">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>   
<book name="Basic XSLT">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>
<book name="Basic Java">
    <type>Tutorial</type>
    <grouping>A</grouping>
</book>
</books>

2。然后选择其他&lt;book&gt; 节点没有“教程”的&lt;type&gt; 并且与#1 中选择的@name 不同,输出仅为:

<books>
<book name="Web Service">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
<book name="C Programming">
    <type>Educational</type>
    <grouping>A</grouping>
</book>
</books>

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    对于第一个查询:

    <xsl:apply-templates select="/books/book[type='Tutorial']"/>
    

    对于第二个查询:

    <xsl:apply-templates select="/books/book[type!='Tutorial']"/>
    

    那么你需要合适的模板来处理它们:

    <xsl:template match="/books/book[type='Tutorial']">
       Do Something...
    </xsl:template>
    

    最后是检查当前节点是否也有教程节点

    <xsl:template match="/books/book[type!='Tutorial']">
       <xsl:variable name="bookname">
          <xsl:value-of select="@name"/>
       </xsl:variable>
       <xsl:if test="count('/books/book[@name=$bookname and type='Tutorial']')=0">
          Do Something...
       </xsl:if>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      这是一个完整的解决方案

      <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output omit-xml-declaration="yes" indent="yes"/>
       <xsl:strip-space elements="*"/>
      
       <xsl:template match="node()|@*">
        <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
       </xsl:template>
      
       <xsl:template match="/*">
        <xsl:variable name="vTutBooks" select="book[type = 'Tutorial']"/>
        <books>
         <xsl:apply-templates select="$vTutBooks"/>
        </books>
      
        <books>
         <xsl:apply-templates select=
         "book[not(type = 'Tutorial')
             and
               not(@name = $vTutBooks/@name)
              ]"/>
        </books>
       </xsl:template>
      </xsl:stylesheet>
      

      当此转换应用于提供的 XML 文档时

      <books>
          <book name="Basic XML">
              <type>Educational</type>
              <grouping>A</grouping>
          </book>
          <book name="Basic XML">
              <type>Tutorial</type>
              <grouping>A</grouping>
          </book>
          <book name="Basic XSLT">
              <type>Educational</type>
              <grouping>A</grouping>
          </book>
          <book name="Basic XSLT">
              <type>Tutorial</type>
              <grouping>A</grouping>
          </book>
          <book name="Basic Java">
              <type>Educational</type>
              <grouping>A</grouping>
          </book>
          <book name="Basic Java">
              <type>Tutorial</type>
              <grouping>A</grouping>
          </book>
          <book name="Web Service">
              <type>Educational</type>
              <grouping>A</grouping>
          </book>
          <book name="C Programming">
              <type>Educational</type>
              <grouping>A</grouping>
          </book>
      </books>
      

      产生想要的正确结果

      <books>
         <book name="Basic XML">
            <type>Tutorial</type>
            <grouping>A</grouping>
         </book>
         <book name="Basic XSLT">
            <type>Tutorial</type>
            <grouping>A</grouping>
         </book>
         <book name="Basic Java">
            <type>Tutorial</type>
            <grouping>A</grouping>
         </book>
      </books>
      <books>
         <book name="Web Service">
            <type>Educational</type>
            <grouping>A</grouping>
         </book>
         <book name="C Programming">
            <type>Educational</type>
            <grouping>A</grouping>
         </book>
      </books>
      

      【讨论】:

        猜你喜欢
        • 2012-05-02
        • 1970-01-01
        • 1970-01-01
        • 2018-06-13
        • 2011-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        相关资源
        最近更新 更多