简体   繁体   English

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

[英]XSLT Transform of Elements that are Key Value

I have this XML: 我有这个XML:

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

That needs to be transformed into this XML, with a special asterix as the starting character as well: 需要将其转换为XML,并以特殊的星号作为起始字符:

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

I've tried several variations of xslt transforms, and I'm all over the place. 我已经尝试了xslt转换的几种变体,并且到处都是。 This is my latest hack. 这是我最新的技巧。 Fiddling with this in the XML tool right now... 现在在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>

Any xslt experts with some guidance on this? 有xslt专家对此有一些指导吗? Thanks! 谢谢!

Here's what you can do with XSLT 1.0: 使用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>

Given your input it produces: 根据您的输入,它将产生:

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

You can do more clever with XSLT 2.0 exploiting the separator attribute of the xsl:value-of as was explained in a similar example: concatenation two fields in xsl by Dimitre 您可以利用XSLT 2.0的xsl:value-of separator属性来做更聪明的事情xsl:value-of如在一个类似示例中所解释的:Dimitre 在xsl中串联两个字段

I. This simple and efficient XSLT 1.0 transformation (no variables and no post-processing of results): 一,简单有效的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>

when applied on the provided XML document: 当应用于提供的XML文档时:

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

produces the wanted, correct result: 产生想要的正确结果:

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

II. 二。 XSLT 2.0 Solution: 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