XSLT副本但更改值

时间:2013-05-19 11:35:08

标签: xml xslt

我有一个XML文件,它有非标准的html元素,如下所示: 注意:段落已经缩短了。

                 <content>
                      Sacrifice. It's Mass Effect 3's major theme...
                      <p />
                      Mass Effect was about time and place; you d...
                      <p />
                      Mass Effect 3 is focused more on plot th...
                      <p />
                      <img url="me3_img1.jpg">Plenty of games feat...</img>
                      Like Star Wars, Mass Effect 3 is an inc...
                      <p />
                      The reapers aren't your only adver...
                      <p />
                      <img url="me3_img2.jpg">If looks could kill..</img>
                      The series' focus on player choice is as vi...
                      <p />
                      This intense narrative is met wi...
                      <p />
                      <img url="me3_img3.jpg">The sun is sett...</img>
                      These are exquis...
                 </content>

每个段落之间都会有差距。 每张图片必须出现在内容之间的段落之间,必须还有一个标题,内容介于两者之间 另请注意,它有一个url而不是src,因此必须更改。

我当前的xslt只有copy-of和css修复了布局,但img没有显示,因为我无法将url更改为src,以及设置标题。

请帮忙。

更新

<xsl:for-each select='./review/content/child::*'>
                          <xsl:value-of select="name()"/>
                          <xsl:choose>
                            <xsl:when test="name()='p'">
                              <p />
                            </xsl:when>
                            <xsl:when test="name()='img'">
                              <div class="reviewImage">
                                <img>
                                  <xsl:attribute name="src">
                                    <xsl:value-of select="./@url"/>
                                  </xsl:attribute>
                                </img>
                                <xsl:value-of select="."/>
                              </div>                                
                            </xsl:when>
                            <xsl:otherwise>
                              <xsl:value-of select="."/>
                            </xsl:otherwise>
                          </xsl:choose>
                        </xsl:for-each>

它没有输出任何文字()。现在可以对图像和标题进行排序。

找到解决方案:

                       <xsl:for-each select='./review/content/node()'>
                          <xsl:choose>
                            <xsl:when test="name()='p'">
                              <p />
                            </xsl:when>
                            <xsl:when test="name()='img'">
                              <div class="reviewImage">
                                <img>
                                  <xsl:attribute name="src">
                                    <xsl:value-of select="./@url"/>
                                  </xsl:attribute>
                                </img>
                                <xsl:value-of select="."/>
                              </div>                                
                            </xsl:when>
                            <xsl:otherwise>
                              <xsl:value-of select="."/>
                            </xsl:otherwise>
                          </xsl:choose>
                        </xsl:for-each>

2 个答案:

答案 0 :(得分:0)

copy-of的目的是将内容从输入树复制到输出树。

为了在输入树和输出树之间进行更改,您需要使用apply-templatestemplate

如果您希望在输出树上输出与输入树上的名称相同但内容不同的元素,则可以在模板中使用copy,并使用适当的内容填充“副本”

答案 1 :(得分:0)

如果您想复制某些内容并更改其中的一部分,答案几乎总是将身份模板与其他模板一起使用。 copy-of无法做到这一点,我尽可能地避免copy-of

以下是您可以按照自己的描述进行操作的方式(当您说“标题”时,我不确定您是指alt还是title属性,因此我将使用两者:

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

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="content/img">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:attribute name="alt">
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:attribute name="title">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="content/img/@url">
    <xsl:attribute name="src">
      <xsl:value-of select="." />
    </xsl:attribute>
   </xsl:template>
</xsl:stylesheet>

在样本输入上运行此操作时,结果为:

<content>
  Sacrifice. It's Mass Effect 3's major theme...
  <p />
  Mass Effect was about time and place; you d...
  <p />
  Mass Effect 3 is focused more on plot th...
  <p />
  <img src="me3_img1.jpg" alt="Plenty of games feat..." title="Plenty of games feat..." />
  Like Star Wars, Mass Effect 3 is an inc...
  <p />
  The reapers aren't your only adver...
  <p />
  <img src="me3_img2.jpg" alt="If looks could kill.." title="If looks could kill.." />
  The series' focus on player choice is as vi...
  <p />
  This intense narrative is met wi...
  <p />
  <img src="me3_img3.jpg" alt="The sun is sett..." title="The sun is sett..." />
  These are exquis...
</content>