-1
I have a below XML.
XML-
<Book>
 <Title>blahblah</Title>
 <Title>
  <subtitle>bcdf</subtitle>
  <subtitle>fgdh</subtitle>asdfg
 </Title>
 <Title>
  <subtitle>bcdf</subtitle>jhuk
  <subtitle>xyza</subtitle>refsdw
 </Title>
<Title>
 <subtitle>bcdf</subtitle>fdgfjhdc
 <subtitle>trey</subtitle>
</Title>

我想通过 xslt 获得以下输出,谁能帮我解决这个问题。

<Title>blahblah</Title>
<Title subtitle="bcdffgdh">asdfg</Title>
<Title subtitle="bcdfxyza">jhukrefsdw</Title>
<Title subtitle="bcdftrey">fdgfjhdc</Title>
4

1 回答 1

0

这个 XSL 给定了您的输入(当然,它使用结束书标签使其有效,并用相同的标签包围输出......只是 100 种方法来完成您所要求的事情的一个示例,它甚至可能不正确,因为在您尝试的事情可以帮助其他人了解您真正在寻找什么以及所有不同的可能性。

这是 XSL:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    <xsl:template match="/">
        <book>
            <xsl:apply-templates/>
        </book>
    </xsl:template>    
    <xsl:template match="Title[child::subtitle]">
        <Title>
        <xsl:attribute name="subtitle">
            <xsl:for-each select="subtitle">
                <xsl:value-of select="text()"/>    
            </xsl:for-each>
        </xsl:attribute>
            <xsl:variable name="mixedtext">
                <xsl:apply-templates select="descendant::text()[not(parent::subtitle)]"/>    
            </xsl:variable>
            <xsl:value-of select="translate(normalize-space($mixedtext),' ','')"/>
        </Title>
    </xsl:template>
        <xsl:template match="Title">
            <Title>
              <xsl:value-of select="."/>
            </Title>
        </xsl:template>
    </xsl:stylesheet>

这是输出:

<book>
    <Title>blahblah</Title>
    <Title subtitle="bcdffgdh">asdfg</Title>
    <Title subtitle="bcdfxyza">jhukrefsdw</Title>
    <Title subtitle="bcdftrey">fdgfjhdc</Title>
</book>

正如你所问的那样(顺便说一句,并不像你在评论中所说的那么难)。花了2分钟。

于 2013-11-12T07:23:10.003 回答