手动创建XSLT

手动创建XSLT,xslt,xml-parsing,Xslt,Xml Parsing,我对XSL概念非常陌生,我正在尝试为下面的XML创建XSLT <?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<

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

<?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,这一切都非常混乱。XSLT生成具有各种属性的
元素,但在所需的输出中没有此类元素或属性。事实上,您的示例代码似乎与所需的输出完全没有关系

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

  • 输出
    行的子元素的字符串值

  • 如果
    @m1
    大于前面的
    @m1
    ,请在字符串值前面加“]”

  • 如果
    @m1
    等于前面的
    @m1
    ,则在其前面加上“\”

  • 如果没有
    @m1
    ,请在其前面加一个空格

如果我的猜测接近目标,那么解决方案如下所示:

<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>


提供的xslt将xml转换为xml,而不是xml转换为文本。是否使用了后续变压器?你在找
xsl:text
?@Arunkumar你能解释一下你用来获取
]
和\输出的逻辑吗?@Mitch我没有使用任何其他转换器。正如我告诉你的,我对XSL非常陌生,因此,如果通过使用XSL:text可以实现我请求的输出,那么我非常高兴。@LingamurthyCS我不使用任何逻辑,实际上我不知道。我希望输出为1234 A\321\654\098]B]C\123]5@Arunkumar你怎么能指望有人在不知道你想要实现输出的逻辑的情况下修复你的代码呢?
1234 A 321 654 098 B C 123 5
<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>