2017-07-10 6 views
0

xmlドキュメントに<xsl:apply-templates select="...."を適用すると問題が発生します。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出力に含まれていることを確認するにはどうすればよいですか?

EDIT:テンプレート正しく属性値をコピーしません:あなたの(下記参照EDIT)がいるようです

<!-- 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

:Soltutionはこのでした。

ので(調整書式)次のような結果を得るためにあなたのXSLTファイルにアイデンティティテンプレート

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

を追加します。

<?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

関連する問題