【问题标题】:XSLT transformation of comma separated xml element逗号分隔的 xml 元素的 XSLT 转换
【发布时间】:2014-03-11 18:17:07
【问题描述】:

我有一个类似这个例子的 xml 文档(如图所示带有空格)

<block>
 <Text>FirstName: Bob, LastName: Smith, PhoneNumber: 12345</Text>
<block>

并希望在 XSLT 中进行转换,以便 xml 变为

<block>
 <Text>
    <FirstName>Bob</FirstName>
    <LastName>Smith</LastName>
    <PhoneNumber>12345</Phonenumber>
 </Text>
<block>

非常感谢任何帮助,非常感谢!

【问题讨论】:

    标签: xml xslt transform


    【解决方案1】:

    下面的样式表使用命名递归模板作为文本内容的标记器。它读取逗号之前的第一个文本并创建一个包含分号之前的字符串的元素,其中包含分号之后的字符串。然后它再次使用逗号后面的字符串调用该方法,直到找不到更多的逗号并创建最后一个元素。

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    
        <xsl:template match="block">
            <xsl:copy>
                <xsl:apply-templates />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Text">
            <xsl:copy>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="."/>
                    <xsl:with-param name="separator" select="','" />
                </xsl:call-template>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template name="tokenize">
            <xsl:param name="text"/>
            <xsl:param name="separator"/>
            <xsl:choose>
                <xsl:when test="not(contains($text, $separator))">
                    <xsl:element name="{substring-before($text, ':')}">
                        <xsl:value-of select="substring-after($text, ': ')"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="{substring-before($text, ':')}">
                        <xsl:value-of select="substring-before(substring-after($text, ': '), $separator)"/>
                    </xsl:element>
                    <xsl:call-template name="tokenize">
                        <xsl:with-param name="text" select="substring-after($text, concat($separator, ' '))"/>
                        <xsl:with-param name="separator" select="','" />
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 干杯所有,感谢您的建议,不幸的是我被限制使用 xslt 1.0 而不是 2.0。
    【解决方案2】:

    你可以使用这个样式表:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Text">
        <xsl:copy>
            <xsl:for-each select="tokenize(text(), ',')">
                <xsl:element name="{normalize-space(tokenize(.,':')[1])}">
                    <xsl:value-of select="normalize-space(tokenize(.,':')[2])"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案3】:

      一个 XSLT 2.0 解决方案,它使用 tokenize() 函数在 ',' 上拆分并生成所需的内容:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
          <xsl:output indent="yes"/>
      
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="Text">
              <xsl:copy>
                  <xsl:for-each select="tokenize(., ',\s?')">
                      <xsl:element name="{substring-before(current(), ': ')}">
                          <xsl:value-of select="substring-after(., ': ')"/>
                      </xsl:element>
                  </xsl:for-each>
              </xsl:copy>
          </xsl:template>
      
      </xsl:stylesheet>
      

      一个使用递归模板的XSLT 1.0解决方案:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
          <xsl:output indent="yes"/>
      
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="Text">
              <xsl:copy>
                  <xsl:call-template name="split"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template name="split">
              <xsl:param name="val" select="."/>
              <xsl:param name="delimiter" select="','"/>
              <xsl:if test="string-length($val) and contains($val, ':')">
                  <xsl:variable name="item" select="substring-before(concat($val, $delimiter), $delimiter)"/>
                  <xsl:element name="{substring-before($item, ': ')}">
                      <xsl:value-of select="substring-after($item, ': ')"/>
                  </xsl:element>
                  <xsl:call-template name="split">
                      <xsl:with-param name="val" select="substring-after($val, $delimiter)"/>
                      <xsl:with-param name="delimiter" select="$delimiter"/>
                  </xsl:call-template>
              </xsl:if>
          </xsl:template>
      </xsl:stylesheet>
      

      【讨论】:

        【解决方案4】:

        这是使用 XSLT 2.0 的一个

        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version="2.0">
        
            <xsl:output omit-xml-declaration="yes"/>
        
            <xsl:template match="node()|@*">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>
                </xsl:copy>
            </xsl:template>
        
            <xsl:template match="Text">
                <xsl:copy>
                    <xsl:for-each select="tokenize(., ', ')">
                        <xsl:element name="{substring-before(., ':')}">
                            <xsl:value-of select="substring-after(., ': ')"/>
                        </xsl:element>
                    </xsl:for-each>
                    </xsl:copy>
            </xsl:template>
        
        </xsl:stylesheet>
        

        当应用于您的输入 XML 时,输出为:

        <block>
            <Text>
                <FirstName>Bob</FirstName>
                <LastName>Smith</LastName>
                <PhoneNumber>12345</PhoneNumber>
            </Text>
        </block>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-30
          • 1970-01-01
          • 2021-11-08
          • 2015-10-25
          • 2011-03-04
          相关资源
          最近更新 更多