首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手动创建XSLT

手动创建XSLT
EN

Stack Overflow用户
提问于 2015-11-09 13:48:39
回答 1查看 62关注 0票数 0

我对XSL概念非常陌生,我正在尝试为下面的XML创建一个XSLT,

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<row>
  <c1>1234</c1>
  <c2>A</c2>
  <c2 m="1" s="2">321</c2>
  <c2 m="1" s="3">654</c2>
  <c2 m="1" s="4">098</c2>
  <c2 m="2">B</c2>
  <c2 m="3">C</c2>
  <c2 m="3" s="2">123</c2>
  <c2 m="4">5</c2>
  <c3 />
</row>

如果使用XSL进行转换,那么输出应该如下所示:

1234 A\321\654\098]B]C\123]5

我试着创建我自己的,如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="row">
    <array />
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="c1">
    <data>
      <xsl:attribute name="attribute">1</xsl:attribute>
      <xsl:attribute name="value">
        <xsl:number level="single" />
      </xsl:attribute>
      <xsl:attribute name="subvalue">1</xsl:attribute>
      <xsl:value-of select="." />
    </data>
  </xsl:template>
  <xsl:template match="c2">
    <data>
      <xsl:attribute name="attribute">1</xsl:attribute>
      <xsl:attribute name="value">
        <xsl:number level="single" />
      </xsl:attribute>
      <xsl:attribute name="subvalue">1</xsl:attribute>
      <xsl:value-of select="." />
    </data>
  </xsl:template>
</xsl:stylesheet>

但是我得到的输出如下,

代码语言:javascript
复制
1234 A 321 654 098 B C 123 5

请帮助我创建XSL

EN

回答 1

Stack Overflow用户

发布于 2015-11-09 16:59:30

这一切都非常令人困惑。您的XSLT生成具有各种属性的<array><data>元素,但是在您想要的输出中没有这样的元素或属性。实际上,您的示例代码似乎与您想要的输出完全没有关系。

从一个例子中对需求进行逆向工程总是很困难的,但我尝试这样做:

  • 输出row元素的子元素的字符串值
  • 如果@m1大于上一个@m1,则在字符串值之前加上“”
  • 如果D11等于上一个D12,在其前面加上“\”H213H114如果没有D15,则在其前面加上一个空格。

F217

如果我的猜测接近目标,那么解决方案应该是这样的:

代码语言:javascript
复制
<xsl:template match="row">
  <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="row/*[@m1 > preceding-sibling::*[1]/@m1]">
  <xsl:value-of select="concat(']', .)"/>
</xsl:template>

<xsl:template match="row/*[@m1 = preceding-sibling::*[1]/@m1]">
  <xsl:value-of select="concat('\', .)"/>
</xsl:template>

<xsl:template match="row/*[not(@m1)]">
  <xsl:value-of select="concat(' ', .)"/>
</xsl:template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33603193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档