当我将<xsl:apply-templates select="...."
应用于我的xml文档时,我遇到了问题。
XML的摘录是:
<?xml version="1.0" encoding="utf-8"?>
<body>
<body.content>
<body.content.text>
<p />
<p>Auckland is expected to come to a standstill on Thursday when Team New Zealand’s triumphant America’s Cup crew return from Bermuda for a victory parade. The All Blacks present themselves as proud supporters of their Kiwi sailing cousins but recent history suggests that they cannot afford to get caught up in all the revelry days before their decisive Test against the British & Irish Lions at Eden Park.</p><p>Last November, the All Blacks were in Chicago when the Cubs ended a 108-year wait for baseball’s World Series title and the players allowed themselves to be swept up in the carnival atmosphere. They took to the streets with five million locals to experience the tickertape parade and, in the words of Steve Hansen, became “tourists rather than a team on tour”. On the Saturday, at Soldier Field, they were defeated by Ireland. “There was an attitudinal problem,” Hansen, the head coach, said.</p>
<block class="pullquote">
<p>They fronted up in defence, closed up that tight space and we didn’t react really well</p>
</block><p>Hansen may sense no such issue now but the Ireland game will still be mentioned frequently by the All Blacks this week because it offers them a rare frame of reference for handling an international defeat. It was New Zealand’s first since August 2015, before the last World Cup, and their response was one of anger. A fortnight later they took it out on Ireland in the Dublin rematch.</p><p>The emotion after Saturday’s 24-21 defeat by the Lions has been much the same — Israel Dagg and Brodie Retallick have both talked of anger — but the key lesson that New Zealand learnt from the Aviva Stadium experience two weeks after the Chicago defeat was not to be consumed by it.</p><p>New Zealand bludgeoned their way to a 21-9 win on that occasion but made many of the same mistakes that they identified in the game last Saturday. In putting the accent on physicality, which they were forced to do in Wellington given the intensity of the Lions’ assault, they missed opportunities.</p><p>“We want to do it differently [from how we reacted between Chicago and Dublin] ,” Aaron Smith, the scrum half, said yesterday. “We turned up with all the fight but we didn’t see as much as we wanted to and didn’t perform as well as we wanted to. We won — but we want to take the learnings from that.”</p><p>Seán O’Brien’s availability for the third Test is huge in ways that will not necessarily be obvious to thousands of red-clad supporters at Eden Park. His contributions around the field have been eye-catching so far, including a lung-bursting 90-metre support line that allowed him to finish off the Lions’ spectacular try in the first Test.</p><p>No Lions forward has carried more often (17 times) or as far (56 metres) as O’Brien over the two Tests. Only Maro Itoje made more tackles than him in Wellington, where he combined with Sam Warburton to produce a top-class defensive effort at the breakdown.</p><p>But O’Brien, now 30, is also a key leader in the team. Graham Rowntree, the forwards coach, describes him as the “barometer” of the squad. Tadhg Furlong, who comes from a similar farming background to O’Brien, said that his influence on the squad could not be underestimated, which is why the decision taken yesterday by a disciplinary committee to dismiss a foul play citing was so important to the Lions.</p><p>“His influence is huge,” Furlong said. “Seánie’s a big leader for us. I don’t know if you have access to the referee microphone, but you can always hear him in his high-pitched tone, shouting away and organising the lads.</p><p>“He’s very vocal, leads the team, talks at team meetings and he leads a lot by his example. I’ve been lucky to come up under his tutelage, in a way, at Leinster, because we’ve quite similar backgrounds in Ireland and on to this. I can’t speak highly enough of him as a person or as a player and how important he is to this group.”</p><p>While O’Brien was one of several stand-out Lions on Saturday, Smith would not argue with Warren Gatland’s claim that the Lions were yet to be “stressed” by the All Blacks’ attacking game in either Test. “After Saturday’s effort, you would have to say exactly that. They fronted up in defence, closed up that tight space and we didn’t react really well,” Smith said.</p><p>“There were so many opportunities that we didn’t see, and that is disappointing. There was a lot more space than we thought.</p><p>“We didn’t maybe adapt as well as we would have hoped and how we usually do. We usually seem to find a way, and our bench comes on and finishes really strong.</p><p>“We have learnt a lot. There were opportunities there. Those opportunities will present themselves next Saturday and we’ve got to be good enough to see them and take them. I am really excited about what we can do this week back at Eden Park.”</p><p>New Zealand have not lost successive games since 2011, before Hansen took charge in 2012. The balance they are looking to strike this week is to channel that anger into their performance and to match the Lions’ physicality while remaining cool-headed and wide-eyed enough to read the opportunities.</p><p>“The physicality and the intensity of the first two matches have been huge, especially from them on Saturday,” Retallick, the lock, said. “I thought they were a lot more physical and confrontational than they were in week one. We need to sort that out and make sure we go back at it this Saturday, because we can’t let it happen again.</p><p>“It is 1-1 in the series. It’s now or never. Throughout the series, we’ve touched on the history and the different tours that have come to New Zealand.</p><p>“This is a once-in-a-lifetime opportunity. The history is not going to help us now, it is just what we are going to do to create our own history.”</p><p>If they succeed at Eden Park, where New Zealand are unbeaten since 1994, that will be the time when the All Blacks allow themselves to get swept up in the national celebrations. Not before.</p><p />
</body.content.text>
</body.content>
</body>
我试图在上面应用的XSL是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:attribute>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="p[not(normalize-space())]" />
<xsl:template match="/nitf">
<xNLAml>
<SourceMetaData>
<Publisher>News International</Publisher>
<Title>timesonline.co.uk</Title>
<ArticleURI>
<xsl:value-of select="head/pubdata/@ex-ref" />
</ArticleURI>
</SourceMetaData>
<Article>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<DescriptiveMetaData>
<Version />
<AVE />
<WordCount />
<CharacterCount />
</DescriptiveMetaData>
</Article>
<DataContent>
<Headline>
<xsl:value-of select="head/title[@type='main']" />
</Headline>
<Slugline>
<xsl:value-of select="body/body.content/subheadline" />
</Slugline>
<Byline>
<xsl:choose>
<xsl:when test="body/body.head/byline/person/name.given">
<xsl:value-of select="normalize-space(body/body.head/byline/person/name.given)" />
</xsl:when>
<xsl:when test="body/body.head/byline/person/pre">
<xsl:value-of select="normalize-space(body/body.head/byline/person/pre)" />
</xsl:when>
</xsl:choose>
<xsl:if test="body/body.head/byline/person/function">
<xsl:value-of select="normalize-space(concat(', ', body/body.head/byline/person/function))" />
</xsl:if>
</Byline>
<Section>
<xsl:value-of select="head/pubdata/@position.section" />
</Section>
<ArticleCredit>
<xsl:value-of select="head/docdata/del-list/from-src/@src-name" />
</ArticleCredit>
<PublishedDateTime>
<xsl:choose>
<xsl:when test="head/meta[@name='editionPublishDate.nla']/@content">
<xsl:value-of select="head/meta[@name='editionPublishDate.nla']/@content" />
</xsl:when>
<xsl:otherwise>1900-01-01 00:00:00</xsl:otherwise>
</xsl:choose>
</PublishedDateTime>
<OriginalPublishedDateTime>
<xsl:value-of select="head/pubdata/@date.publication.nla" />
</OriginalPublishedDateTime>
<Body>
<xsl:apply-templates select="body/body.content/body.content.text/*" />
</Body>
<xsl:for-each select="//nitf/body/body.content/media">
<xsl:element name="Image">
<xsl:attribute name="source">
<xsl:value-of select="media-reference/@source.nla" />
</xsl:attribute>
<xsl:attribute name="caption">
<xsl:value-of select="media-caption" />
</xsl:attribute>
<xsl:attribute name="credit">
<xsl:value-of select="media-credit" />
</xsl:attribute>
<xsl:attribute name="alt-text">
<xsl:value-of select="media-caption" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</DataContent>
</xNLAml>
</xsl:template>
</xsl:stylesheet>
由于某种原因,块元素中@class的值(即“quote”)不会出现。我得到的结果是:
<p>Here's some text</p>
<block class="">
<p>This is quote text</p>
</block>
如何确保我的XML输出中包含“quote”值?
编辑:Soltution就是这样:
<!-- fix for attribute values not being carried over -->
<xsl:template match="@*" priority="1">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
答案 0 :(得分:0)
您的(编辑:见下文)模板似乎无法正确复制属性值。
所以添加一个身份模板
<xsl:template match="node()|@*" priority="1"> <!-- see below -->
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
到您的XSLT文件以获得以下结果(格式调整):
<?xml version="1.0" encoding="utf-8"?>
<body>
<p>Here's some text</p>
<block class="quote">
<p>This is quote text</p>
</block>
</body>
此模板中的属性值会从XSLT中丢失,而XSLT不会复制该值。
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:attribute>
</xsl:template>
因此,请从上面的XSLT代码中删除此模板,或者从此答案中添加身份模板。