【问题标题】:XSLT for wrapping immediate following-siblings of certain conditionXSLT 用于包装特定条件下的直系兄弟姐妹
【发布时间】:2023-02-16 20:16:06
【问题描述】:

我有以下源 XML

<root>
  <i type="r"/>
  <i type="s"/>
  <i type="r"/>
  <i type="r"/>
  <i type="s"/>
  <i type="r"/>
  <i type="s"/>
</root>

我的目标是将所有事件与所有即时following-siblings (1..n) of type="r".

无包装:

  • type="r" 节点没有前面的 type="s" 节点
  • type="s" 节点没有直接跟随 type="r" 节点

预期输出:

<i type="r"/>
<wrap>
  <i type="s"/>
  <i type="r"/>
  <i type="r"/>
</wrap>
<wrap>
  <i type="s"/>
  <i type="r"/>
</wrap>
<i type="s"/>

我一直在尝试使用以下 XSLT sn-p 解决问题:

<xsl:for-each select="./i">
  <xsl:choose>
    <xsl:when test="current()[./@type='r' and count(preceding-sibling::i[@type='s']) = 0]">
      <!-- Processing w/o wrap -->
      <xsl:apply-templates select="current()" mode="fill"/>
    </xsl:when>
    <xsl:when test="current()[./@type='s' and following-sibling::i[@type='s']]">
      <!-- Processing w/o wrap -->
      <xsl:apply-templates select="current()" mode="fill"/>
    </xsl:when>
    <xsl:when test="current()[./@type='r' and count(preceding-sibling::i[@type='s']) > 0]">
      <!-- Do nothing -->
    </xsl:when>
    <xsl:when test="current()[./@type='s' and following-sibling::i[1][@type='r']]">
      <wrap>
        <xsl:apply-templates select="current() | //i[@type='r' and preceding-sibling::i[@type='s']" mode="fill"/>
      </wrap>
    </xsl:when>
  </xsl:choose>
</xsl:for-each>

我总是无法在 .

需要注意的是后续的模板都是在处理所有的节点。由于与问题本身无关,这些模板已被省略。

【问题讨论】:

    标签: xpath xslt-1.0


    【解决方案1】:

    考虑以下示例:

    XML

    <root>
      <i type="r">1</i>
      <i type="s">2</i>
      <i type="r">3</i>
      <i type="r">4</i>
      <i type="s">5</i>
      <i type="r">6</i>
      <i type="s">7</i>
    </root>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:key name="r" match="i[@type='r']" use="generate-id(preceding-sibling::i[@type='s'][1])" />
    
    <xsl:template match="/root">
        <xsl:copy>
            <xsl:copy-of select="key('r', '')"/>
            <xsl:for-each select="i[@type='s']">
                <xsl:variable name="r" select="key('r', generate-id())" />
                <xsl:choose>
                    <xsl:when test="$r">
                        <wrap>
                            <xsl:copy-of select=". | $r"/>
                        </wrap>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    结果

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <i type="r">1</i>
       <wrap>
          <i type="s">2</i>
          <i type="r">3</i>
          <i type="r">4</i>
       </wrap>
       <wrap>
          <i type="s">5</i>
          <i type="r">6</i>
       </wrap>
       <i type="s">7</i>
    </root>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多