Xml 考虑到相同的属性值,需要删除第一个标题

Xml 考虑到相同的属性值,需要删除第一个标题,xml,xslt-2.0,xslt-3.0,Xml,Xslt 2.0,Xslt 3.0,我需要从body元素中删除第一个title,因为这两个title的outputclass值相同 输入XML: <topic> <title outputclass="header">Sample</title> <topic> <title outputclass="header">Test</title> <topic>

我需要从body元素中删除第一个title,因为这两个title的outputclass值相同

输入XML:

<topic>
   <title outputclass="header">Sample</title>
   <topic>
      <title outputclass="header">Test</title>
      <topic>
         <title outputclass="section">Section</title>
            <body>
               <p outputclass="normal">Solution</p>
            </body>
      </topic>
   </topic>
</topic>

样品
试验
部分

解决方案

XSLT我有:

<xsl:template match="/*">
    <document>
       <head>
          <title><xsl:value-of select="title[@outputclass='header']"/></title>
       </head>
       <body>
          <xsl:apply-templates/>
       </body>
    </document>
</xsl:template>

<xsl:template match="topic/title[@outputclass='header'][1]"/>

<xsl:template match="topic">
    <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="body">
    <xsl:apply-templates/>
    </xsl:template>

<xsl:template match="title | p">
   <p>
      <xsl:apply-templates/>
   </p>
</xsl:template>


预期产出:

<document>
   <head><title>Sample</title></head>
   <body>
      <p>Test</p>
      <p>Section</p>
      <p>Solution</p>
   </body>
</document>

样品
试验

部分

解决方案


我需要删除第一个标题,该标题仅考虑相同的属性
outputclass

您可以声明顶级变量或参数

<xsl:param name="first-output-header" select="/*/descendant::title[@outputclass = 'header'][1]"/>

然后使用

<xsl:template match="$first-output-header"/>

至少在XSLT3中。我认为这在XSLT2中也是可能的,但需要深入研究规范或找到实现“旧”版本的东西

XSLT3中的另一个选项是使用累加器对“头”进行计数并检查值,例如

<xsl:accumulator name="header-count" as="xs:integer" initial-value="0">
    <xsl:accumulator-rule
      match="topic/title[@outputclass = 'header']"
      select="$value + 1"/>
</xsl:accumulator>

<xsl:mode use-accumulators="header-count"/>

<xsl:template match="topic/title[@outputclass = 'header'][accumulator-before('header-count') = 1]"/>

这也适用于流媒体