2017-07-31 26 views
1

長時間のlurker、初めてのポスター。XSLT:属性が存在するかどうかの確認

私はXSLTファイルを構築しようとしている:ノードが存在する場合

  1. チェックし、それが「0」の場合

  2. でそれを埋めるためにしていない場合存在しないノードは、作るためにチェックし、各属性が存在している、とあります場合、私は、私は<xsl:if/>でこれを行うことができますが、私は避けたいのですが考え出し欠落している属性が「0」

とそれを記入してくださいこの私がチェックしなければならない約56の属性があるからです。ここで

が主なXMLは次のようになります。

<player name="KLINE, Zach" shortname="KLINE, Zach" checkname="KLINE,ZACH" uni="1Q" class="SR" gp="1" code="1Q"> 
    <rush att="1" yds="11" gain="11" loss="0" td="0" long="11"></rush> 
    <pass comp="7" att="11" int="1" yds="110" td="0" long="32" sacks="0" sackyds="0"></pass> 
</player> 
<player name="JORDAN, Jamire" shortname="JORDAN, Jamire" checkname="JORDAN,JAMIRE" uni="1" class="SO" gp="1" code="1"> 
    <rush att="1" yds="1" gain="1" loss="0" td="0" long="1"></rush> 
    <rcv no="5" yds="52" td="0" long="16"></rcv> 
</player> 

出力は次のようになります。

<player name="KLINE, Zach" shortname="KLINE, Zach" checkname="KLINE,ZACH" uni="1Q" class="SR" gp="1" code="1Q"> 
    <rush att="1" yds="11" gain="11" loss="0" td="0" long="11"></rush> 
    <pass comp="7" att="11" int="1" yds="110" td="0" long="32" sacks="0" sackyds="0"></pass> 
    <rcv no="0" yds="0" td="0" long="0"></rcv> 
</player> 
<player name="JORDAN, Jamire" shortname="JORDAN, Jamire" checkname="JORDAN,JAMIRE" uni="1" class="SO" gp="1" code="1"> 
    <rush att="1" yds="1" gain="1" loss="0" td="0" long="1"></rush> 
    <pass comp="0" att="0" int="0" yds="0" td="0" long="0" sacks="0" sackyds="0"></pass> 
    <rcv no="5" yds="52" td="0" long="16"></rcv> 
</player> 
+2

期待私たちに示してください。与えられた例を変換する出力。 –

+0

完了!ありがとうございます – Rob

答えて

1

これはxsl:attribute-setを使用するために理想的なケースのように思えます。以下の簡単な例を考えてみましょう:

XML

<root> 
    <player name="KLINE, Zach"> 
     <rush att="1" yds="11" gain="11"/> 
    </player> 
    <player name="JORDAN, Jamire"> 
     <rcv yds="52" long="16"/> 
    </player> 
    <player name="SMITH, Adam"/> 
</root> 

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:attribute-set name="rush"> 
    <xsl:attribute name="att">0</xsl:attribute> 
    <xsl:attribute name="yds">0</xsl:attribute> 
    <xsl:attribute name="gain">0</xsl:attribute> 
    <xsl:attribute name="loss">0</xsl:attribute> 
    <xsl:attribute name="td">0</xsl:attribute> 
    <xsl:attribute name="long">0</xsl:attribute> 
</xsl:attribute-set> 

<xsl:attribute-set name="rcv"> 
    <xsl:attribute name="no">0</xsl:attribute> 
    <xsl:attribute name="yds">0</xsl:attribute> 
    <xsl:attribute name="td">0</xsl:attribute> 
    <xsl:attribute name="long">0</xsl:attribute> 
</xsl:attribute-set> 

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

<xsl:template match="player"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <rush xsl:use-attribute-sets="rush"> 
      <xsl:apply-templates select="rush/@*"/> 
     </rush> 
     <rcv xsl:use-attribute-sets="rcv"> 
      <xsl:apply-templates select="rcv/@*"/> 
     </rcv> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

結果

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <player name="KLINE, Zach"> 
    <rush att="1" yds="11" gain="11" loss="0" td="0" long="0"/> 
    <rcv no="0" yds="0" td="0" long="0"/> 
    </player> 
    <player name="JORDAN, Jamire"> 
    <rush att="0" yds="0" gain="0" loss="0" td="0" long="0"/> 
    <rcv no="0" yds="52" td="0" long="16"/> 
    </player> 
    <player name="SMITH, Adam"> 
    <rush att="0" yds="0" gain="0" loss="0" td="0" long="0"/> 
    <rcv no="0" yds="0" td="0" long="0"/> 
    </player> 
</root> 
+0

ありがとうございました - それはトリックでした! – Rob

関連する問題