2016-08-11 7 views
0

処理命令の名前を変更すると同時に、一意のid属性を追加したいとします。私の入力は、次のようになります。結果は次のようになります処理命令(xsl)に一意のIDを追加

?xml version="1.0" encoding="utf-8" ?> 
 
<material xml:lang="en-us"> 
 
    <title> 
 
    <?PI_start author="joepublic" comment="Comment #1" ?>Discovering 
 
     <?PI_end?>XML 
 
    </title> 
 
    <related-stuff> 
 
    <?PI_start author="johndoe" comment="Comment #3" ?> 
 
     <a href="otherdoc.xml" /> 
 
     <?PI_end?> 
 
    </related-stuff> 
 
</material>

:私は2つのIDを持っている

お知らせ

<?xml version="1.0" encoding="utf-8"?> 
 
<material xml:lang="en-us"> 
 
    <title> 
 
    <?otherPI_start author="joepublic" comment="Comment #1" id ="1" ?>Discovering 
 
    <?otherPI_end id ="1" ?>XML 
 
    </title> 
 
    <related-stuff> 
 
    <?otherPI_start author="johndoe" comment="Comment #3" id ="2" ?> 
 
    <a href="otherdoc.xml" /> 
 
    <?otherPI_end id ="2"?> 
 
    </related-stuff> 
 
</material>
は、IDを生成しました=」文書内に出現した最初の命令については「1」、2番目の命令については「2」となる。 idが他のPI_end処理命令で繰り返されることにも注意してください。

これを行うxslの一致する声明を特定するお手伝いができますか?あなたは(ゼロから始まる番号を意味します)、現在の処理命令をカウントしない、これを見つけた場合は

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
 
    <xsl:output method="xml" indent="yes" /> 
 
    <xsl:template match="node()|@*"> 
 
    <xsl:copy> 
 
     <xsl:apply-templates select="node()|@*" /> 
 
    </xsl:copy> 
 
    </xsl:template> 
 
    <xsl:template match="processing-instruction('PI_start')"> 
 
    <xsl:processing-instruction name="otherPI_start"> 
 
     author="<xsl:value-of select="substring-before(substring-after(.,'author=&quot;'),'&quot;')"/>" 
 
     comment="<xsl:value-of select="substring-before(substring-after(.,'comment=&quot;'),'&quot;')"/>" 
 
     id="<!-- What should I put here??? -->" 
 
    </xsl:processing-instruction> 
 
    </xsl:template> 
 
    <xsl:template match="processing-instruction('PI_end')"> 
 
    <xsl:processing-instruction name="otherPI_end"> 
 
     id="<!-- What should I put there??? -->" 
 
    </xsl:processing-instruction> 
 
    </xsl:template> 
 
</xsl:stylesheet>

答えて

1

id="<!-- What should I put here??? -->"

<xsl:value-of select="generate-id()"/> 

あなたは<?PI_end?>のidは、前<?PI_start ?>のものと一致する場合は、使用します。

<xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/> 

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="*"/> 

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

<xsl:template match="processing-instruction('PI_start')"> 
    <xsl:processing-instruction name="otherPI_start"> 
     <xsl:value-of select="."/> 
     <xsl:text>id="</xsl:text> 
     <xsl:value-of select="generate-id()"/> 
     <xsl:text>"</xsl:text> 
    </xsl:processing-instruction> 
</xsl:template> 

<xsl:template match="processing-instruction('PI_end')"> 
    <xsl:processing-instruction name="otherPI_end"> 
     <xsl:text>id="</xsl:text> 
     <xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/> 
     <xsl:text>"</xsl:text> 
    </xsl:processing-instruction> 
</xsl:template>  

</xsl:stylesheet> 
1

あなたがここに

<xsl:template match="processing-instruction('PI_start')"> 
<xsl:processing-instruction name="otherPI_start"> 
    author="<xsl:value-of select="substring-before(substring-after(.,'author=&quot;'),'&quot;')"/>" 
    comment="<xsl:value-of select="substring-before(substring-after(.,'comment=&quot;'),'&quot;')"/>" 
    id="<xsl:number count="processing-instruction('PI_start')" level="any" />" 
</xsl:processing-instruction> 
</xsl:template> 

xsl:numberを利用することができ、してみてくださいこれは代わりに....

<xsl:number count="/|processing-instruction('PI_start')" level="any" /> 
+0

なぜ「現在の処理命令はカウントされませんか」と言いますか?私は前にpisに番号がついているのか分からないが、xsl:numberがなぜ要素に番号を付けるのと違うのか分からない。そして、Saxon 6.5.5で 'を使用してスニペットをテストすると、番号は「1」から始まります。 –

+0

http://xsltransform.net/がもう一度ダウンして、私はhttp://www.xslfiddle.net/になりました。これは0で番号を付けました。これをもう少し明確にするために私の答えを書き直しました。 –

+0

ええと、ゼロから始まるのはChromeとlibxsltのバグのようです。 –

関連する問題