【问题标题】:How to transform xml structure using XSLT-1.0如何使用 XSLT-1.0 转换 xml 结构
【发布时间】:2012-05-31 00:43:56
【问题描述】:

我需要改造这个结构

<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>

进入

<A>
<B>value1<B>
<B>value2<B>
</A>

使用 XSLT-1.0 的最佳解决方案是什么? 谢谢!

PS:我试过这个代码:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>   
<xsl:key name="group_a" match="//A" use="B"/> 
<xsl:template match="/Test"> <a-node> <xsl:for-each select="//A"> <b-node> 
<xsl:value-of select="//A/B"/> </b-node> </xsl:for-each> </a-node> 
</xsl:template> 
</xsl:stylesheet> 

但它只返回第一个值:

<?xml version="1.0" encoding="utf-8"?> <a-node mlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value1</b-node> </a-node> 

但我需要:

<?xml version="1.0" encoding="utf-8"?> <a-node xmlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value2</b-node> </a-node>

【问题讨论】:

    标签: xml xslt transform xslt-1.0


    【解决方案1】:

    此转换使用最基本的 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="A[1]">
      <A>
        <xsl:apply-templates select="node()|following-sibling::A/node()"/>
      </A>
     </xsl:template>
     <xsl:template match="A"/>
    </xsl:stylesheet>
    

    当此转换应用于以下 XML 文档时(通过将提供的 XML 片段包装到单个顶部元素中获得 - 以使其成为格式良好的 XML 文档):

    <t>
        <A>
            <B>value1</B>
        </A>
        <A>
            <B>value2</B>
        </A>
    </t>
    

    产生了想要的正确结果:

    <t>
       <A>
          <B>value1</B>
          <B>value2</B>
       </A>
    </t>
    

    【讨论】:

      【解决方案2】:

      既然你似乎需要将所有子节点折叠在一个节点下,那么你不需要在“A”上的foreach,你可以直接移动到“B”子节点

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      version="1.0">
          <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
          <xsl:template match="/">
              <A>
                  <xsl:for-each select="//A/B">
                      <B>
                          <xsl:value-of select="./text()"/>
                      </B>
                  </xsl:for-each>
              </A>
          </xsl:template>
      </xsl:stylesheet>
      

      EDIT 根据@Sean 的评论,请注意 // 永远不应该在现实生活中使用。将 // 替换为您真正的根元素的路径。

      【讨论】:

      • 功能正确但效率低下。最好使用模板样式并避免不必要地使用 // 运算符。
      • @Sean - 当然 - 同意从不使用 // 但别无选择,因为 OP 的 xml 样本没有根元素:)
      【解决方案3】:

      这个样式表 ...

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      version="1.0">
          <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
          <xsl:template match="/">
             <A>
              <xsl:apply-templates select="*/A/B"/>
             </A>
          </xsl:template>
      
          <xsl:template match="B">
            <B><xsl:value-of select="."/></B>
          </xsl:template>
      </xsl:stylesheet>
      

      ...将改变...

      <root>
      <A>
      <B>value1</B>
      </A>
      <A>
      <B>value2</B>
      </A>
      </root>
      

      ...进入这个...

       <A><B>value1</B><B>value2</B></A>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 2013-12-06
        • 1970-01-01
        相关资源
        最近更新 更多