【问题标题】:XSLT: Wrapping a node and following text node with another tagXSLT:用另一个标签包装一个节点并跟随文本节点
【发布时间】:2013-05-09 12:15:49
【问题描述】:

我有下面的 XML 文档,我想使用 XSLT 进行转换。

输入:

<ABS>
  <B>Heading 1</B> 
  text
  <B>Heading 2</B> 
  text
  <B>Heading 3</B> 
  text
  <B>Heading 4</B> 
  text
</ABS>

我需要编写一个转换,以便每个标题及其后面的文本都包含在 &lt;sec&gt; 标记中,如下例所示。

所需的输出:

<ABS>
  <sec>
    <B>Heading 1</B> 
    text
  </sec>
  <sec>
    <B>Heading 2</B> 
    text
  </sec>
  <sec>
    <B>Heading 3</B> 
    text
  </sec>
  <sec>
    <B>Heading 4</B> 
    text
  </sec>    
</ABS>

有人知道我如何使用 XSLT 样式表做到这一点吗?

谢谢

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    请在下面找到 XSLT:

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="ABS">
    <xsl:copy>
        <xsl:for-each select="B">
        <sec><xsl:copy-of select="."/><xsl:value-of select="following-sibling::text()[1]"/></sec>   
        </xsl:for-each>
    </xsl:copy>
    </xsl:template>    
    </xsl:stylesheet>
    

    【讨论】:

    • 这符合问题,但只有当ABAS的子节点只有B节点和文本节点时才有效
    【解决方案2】:

    这是你的解决方案:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:output indent="yes"/>
      <xsl:template match="ABS">
        <ABS>
          <xsl:apply-templates/>
        </ABS>
      </xsl:template>
    
      <xsl:template match="*">
        <xsl:choose>
          <xsl:when test="name()='B'">
            <sec>
              <xsl:copy-of select="."/>
              <xsl:value-of select="following-sibling::text()[1]"/>
            </sec>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy>
              <xsl:copy-of select="."/>
            </xsl:copy>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
      <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <?xml version="1.0"?>
      <xsl:stylesheet
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         version="1.0">
          <xsl:output method="xml" indent="yes"/>
      
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="B">
              <sec>
                  <xsl:copy>
                      <xsl:apply-templates />
                  </xsl:copy>
                  <xsl:value-of select="following-sibling::text()[1]"/>
      
              </sec>
          </xsl:template>
          <xsl:template match="text()[preceding::*[name()='B']]">
      
          </xsl:template>
      </xsl:stylesheet>
      

      这将生成以下输出:

      <?xml version="1.0"?>
      <ABS>
          <sec><B>Heading 1</B>
          text
          </sec><sec><B/>
          text
          </sec><sec><B/>
          text
          </sec><sec><B/>
          text
      </sec></ABS>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-29
        相关资源
        最近更新 更多