2017-07-10 33 views
0

我在應用<xsl:apply-templates select="...."到我的xml文檔時遇到問題。XSLT:應用選擇模板時,元素屬性的值會丟失

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 &amp; 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不會出現。我得到的結果是這樣的:

<p>Here's some text</p> 
<block class=""> 
    <p>This is quote text</p> 
</block> 

如何確保「quote」值包含在我的XML輸出中?

編輯: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> 
+2

請發佈一個可重現的例子,而不是從他們的上下文中取出代碼片段 - 請參閱:[mcve]。 –

+0

@ michael.hor257k我已添加原始碼 – Bodz

+0

友善建議:您希望人們能夠解決您的問題。 「最小,完整和可驗證」這意味着在提供上下文(「完整」)和提供不必要的細節(「最小化」)之間進行平衡。實際上需要多少上述數據來重現問題?我們不需要您的完整代碼,只需重現即可。 – dcsohl

回答

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,它不會複製該值。

<xsl:template match="@*"> 
    <xsl:attribute name="{local-name()}"> 
    <xsl:apply-templates select="@* | node()" /> 
    </xsl:attribute> 
</xsl:template> 

所以,無論是從您的XSLT代碼中刪除此模板上面或從這個答案添加身份模板

+0

如何將這個應用到我的修改代碼?我已經注意到上面的原始 – Bodz

+0

我試圖使用它,但它不起作用 – Bodz

+0

我擴展了我的答案。 – zx485