2016-08-11 49 views
0

我想重命名一個處理指令,同時爲它添加一個唯一的id屬性。我的輸入是這樣的:給處理指令添加一個唯一的id(xsl)

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

結果是這樣的:

<?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的產生,ID =」 1「表示文檔中遇到的第一條指令,第二條表示id =」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:number這裏

<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 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中的一個錯誤。 –