用于分割XML的XSLT

用于分割XML的XSLT,xslt,select,nodes,Xslt,Select,Nodes,我有这样一个XML: <company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> <employee name="karl"/> <employee name="tom"/> <employee name="jim"/> <employee name

我有这样一个XML:

<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>

如何使用XSLT模板仅选择前n个节点(例如3个),以便获得:

<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
</company>
<company> 
    <employee name="john"/> 
    <employee name="sarah"/> 
    <employee name="kim"/> 
</company>

在Oxygen XML编辑器中,我可以使用以下XPath实现这一点:

/company/employee[position() < (last() - count(/company/employee)+4)]
/company/employee[position() < (last() - count(/company/employee)+4)]
/company/employee[position()<(last()-count(/company/employee)+4]
但在这种情况下,我确实需要使用XSLT
感谢您的帮助

试试这个:

<xsl:for-each select="company/employee[position() &lt; 3]">
  ...
</xsl:for-each>

...
这可能适用于
,请尝试以下操作:

<xsl:for-each select="company/employee[position() &lt; 3]">
  ...
</xsl:for-each>

...
这可能适用于

我可以使用以下XPath来
实现以下目标:

/company/employee[position() < (last() - count(/company/employee)+4)]
/company/employee[position() < (last() - count(/company/employee)+4)]
在某种模式下,您可以:

<xsl:template match="employee[4 > position()]">    
...
</xsl:template>

...
参数化也是如此(请记住,在XSLT1.0模式中不能使用参数引用):


...
我可以使用以下XPath来 实现以下目标:

/company/employee[position() < (last() - count(/company/employee)+4)]
/company/employee[position() < (last() - count(/company/employee)+4)]
在某种模式下,您可以:

<xsl:template match="employee[4 > position()]">    
...
</xsl:template>

...
参数化也是如此(请记住,在XSLT1.0模式中不能使用参数引用):


...
如何使用XSLT模板来 仅选择前n个节点,3 例如,我可以得到:

<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
</company>
<company> 
    <employee name="john"/> 
    <employee name="sarah"/> 
    <employee name="kim"/> 
</company>

简短的回答:只需要了解一点XPath和XSLT

完整(但仍然简短)的回答

<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="employee[position() > 3]"/>
</xsl:stylesheet>
<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>
<company>
   <employee name="john"/>
   <employee name="sarah"/>
   <employee name="kim"/>
</company>
此转换

<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="employee[position() > 3]"/>
</xsl:stylesheet>
<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>
<company>
   <employee name="john"/>
   <employee name="sarah"/>
   <employee name="kim"/>
</company>

应用于提供的XML文档时

<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="employee[position() > 3]"/>
</xsl:stylesheet>
<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>
<company>
   <employee name="john"/>
   <employee name="sarah"/>
   <employee name="kim"/>
</company>

生成所需的正确结果

<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="employee[position() > 3]"/>
</xsl:stylesheet>
<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>
<company>
   <employee name="john"/>
   <employee name="sarah"/>
   <employee name="kim"/>
</company>

注意事项

<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="employee[position() > 3]"/>
</xsl:stylesheet>
<company>
    <employee name="john"/>
    <employee name="sarah"/>
    <employee name="kim"/>
    <employee name="karl"/>
    <employee name="tom"/>
    <employee name="jim"/>
    <employee name="sandy"/>
</company>
<company>
   <employee name="john"/>
   <employee name="sarah"/>
   <employee name="kim"/>
</company>
  • 用于“按原样”复制每个节点

  • 只有一个特定模板覆盖标识模板。它匹配节点列表中位置大于3的任何
    employee
    元素。此模板的正文为空,实际上会丢弃匹配的元素

  • 如何使用XSLT模板来 仅选择前n个节点,3 例如,我可以得到:

    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
    </company>
    
    <company> 
        <employee name="john"/> 
        <employee name="sarah"/> 
        <employee name="kim"/> 
    </company>
    
    
    
    简短的回答:只需要了解一点XPath和XSLT

    完整(但仍然简短)的回答

    <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="employee[position() > 3]"/>
    </xsl:stylesheet>
    
    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
        <employee name="karl"/>
        <employee name="tom"/>
        <employee name="jim"/>
        <employee name="sandy"/>
    </company>
    
    <company>
       <employee name="john"/>
       <employee name="sarah"/>
       <employee name="kim"/>
    </company>
    
    此转换

    <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="employee[position() > 3]"/>
    </xsl:stylesheet>
    
    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
        <employee name="karl"/>
        <employee name="tom"/>
        <employee name="jim"/>
        <employee name="sandy"/>
    </company>
    
    <company>
       <employee name="john"/>
       <employee name="sarah"/>
       <employee name="kim"/>
    </company>
    
    
    
    应用于提供的XML文档时

    <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="employee[position() > 3]"/>
    </xsl:stylesheet>
    
    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
        <employee name="karl"/>
        <employee name="tom"/>
        <employee name="jim"/>
        <employee name="sandy"/>
    </company>
    
    <company>
       <employee name="john"/>
       <employee name="sarah"/>
       <employee name="kim"/>
    </company>
    
    
    
    生成所需的正确结果

    <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="employee[position() > 3]"/>
    </xsl:stylesheet>
    
    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
        <employee name="karl"/>
        <employee name="tom"/>
        <employee name="jim"/>
        <employee name="sandy"/>
    </company>
    
    <company>
       <employee name="john"/>
       <employee name="sarah"/>
       <employee name="kim"/>
    </company>
    
    
    
    注意事项

    <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="employee[position() > 3]"/>
    </xsl:stylesheet>
    
    <company>
        <employee name="john"/>
        <employee name="sarah"/>
        <employee name="kim"/>
        <employee name="karl"/>
        <employee name="tom"/>
        <employee name="jim"/>
        <employee name="sandy"/>
    </company>
    
    <company>
       <employee name="john"/>
       <employee name="sarah"/>
       <employee name="kim"/>
    </company>
    
  • 用于“按原样”复制每个节点

  • 只有一个特定模板覆盖标识模板。它匹配节点列表中位置大于3的任何
    employee
    元素。此模板的正文为空,实际上会丢弃匹配的元素


  • 好问题,+1。请参阅我的答案,以获得完整且非常简短的解决方案。:)好问题,+1。请参阅我的答案,以获得完整且非常简短的解决方案。:)