繁体   English   中英

作为关键值的元素的XSLT转换

[英]XSLT Transform of Elements that are Key Value

我有这个XML:

<Year>
  <Movies>
    <Add>
        <Key>movie1</Key>
        <Value>black</Value>
    </Add>
    <Add>
        <Key>movie2</Key>
        <Value>white</Value>
    </Add>
  </Movies>
</Year>

需要将其转换为XML,并以特殊的星号作为起始字符:

<Year>
    <MovieList>*movie1-black,movie2-white<MovieList>
</Year>

我已经尝试了xslt转换的几种变体,并且到处都是。 这是我最新的技巧。 现在在XML工具中解决这个问题...

<xsl:template match="b:Year">
 <xsl:copy>
   <xsl:for-each select="*">
      <xsl:element name="MovieList">
        <xsl:for-each select="./b:Movies/b:Add">
          <xsl:if test="position() = 1">*</xsl:if>
          <xsl:value-of select="./b:Key"/>-<xsl:value-of select="./b:Value"/>
          <xsl:if test="position() != last()">,</xsl:if>
        </xsl:for-each>
      </xsl:element>
   </xsl:for-each>
  <xsl:apply-templates select="node()"/>
 </xsl:copy>
</xsl:template>

有xslt专家对此有一些指导吗? 谢谢!

使用XSLT 1.0可以执行以下操作:

 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

     <xsl:template match="Year/Movies">
        <Year>
          <xsl:variable name="movielist">
            <xsl:for-each select="Add">
              <xsl:value-of select="concat(Key, '-', Value, ',')"/>
            </xsl:for-each>
          </xsl:variable>
          <MovieList>
              <xsl:value-of select="concat('*',substring($movielist, 0, string-length($movielist)))"/>
          </MovieList>
        </Year>
    </xsl:template>
</xsl:stylesheet>

根据您的输入,它将产生:

<Year>
   <MovieList>*movie1-black,movie2-white</MovieList>
</Year>

您可以利用XSLT 2.0的xsl:value-of separator属性来做更聪明的事情xsl:value-of如在一个类似示例中所解释的:Dimitre 在xsl中串联两个字段

一,简单有效的XSLT 1.0转换(无变量,无结果后处理):

<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="/*">
    <Year>
     <xsl:apply-templates/>
    </Year>
 </xsl:template>

 <xsl:template match="Movies">
  <MovieList>
   <xsl:apply-templates/>
  </MovieList>
 </xsl:template>

 <xsl:template match="Add">
  <xsl:value-of select="substring('*,', 2 - (position()=1),1)"/>
  <xsl:value-of select="concat(Key, '-', Value)"/>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的XML文档时:

<Year>
    <Movies>
        <Add>
            <Key>movie1</Key>
            <Value>black</Value>
        </Add>
        <Add>
            <Key>movie2</Key>
            <Value>white</Value>
        </Add>
    </Movies>
</Year>

产生想要的正确结果:

<Year>
   <MovieList>*movie1-black,movie2-white</MovieList>
</Year>

二。 XSLT 2.0解决方案:

<xsl:stylesheet version="2.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="/*">
    <Year>
     <xsl:apply-templates/>
    </Year>
 </xsl:template>

 <xsl:template match="Movies">
  <MovieList>
   <xsl:sequence select="'*'[current()/Add]"/>
   <xsl:value-of select="Add/concat(Key, '-', Value)"
        separator=","/>
  </MovieList>
 </xsl:template>
</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM