簡體   English   中英

XSLT —為根元素生成唯一的ID,並將該值附加到子元素的ID

[英]XSLT — generate unique id for root element and append that value to the id of child elements

我需要:

(1)為根元素生成唯一的id屬性

(2)將id附加到子元素

(3)將任何父元素的名稱和順序附加到子元素的id屬性

**注-我的機器上有一個XML編輯器,可以使用XSLT 2.0,但更喜歡1.0,因為每當我使用Visual Basic運行宏時,我都認為Microsoft xml / xslt處理器只能處理xslt 1.0。 它似乎不喜歡2.0。

源XML示例:

<root>
<segment>
<para>Text of the segment here.</para>
</segment>
<segment>
<para>Text of the segment here.</para>
<para>Text of the segment here.</para>
</segment>
<segment>
<para>Text of the segment here.</para>
<sub_segment>
<para>Text of the segment here.</para>
</sub_segment>
</segment>
</root>

所需的輸出XML:

<root id="idrootx2x1">
<segment id="idrootx2x1.segment.1">
<para id="idrootx2x1.segment.1.para.1">Text of the segment here.</para>
</segment>
<segment id="idrootx2x1.segment.2">
<para id="idrootx2x1.segment.2.para.1">Text of the segment here.</para>
<para id="idrootx2x1.segment.2.para.2">Text of the segment here.</para>
</segment>
<segment id="idrootx2x1.segment.3">
<para id="idrootx2x1.segment.3.para.1">Text of the segment here.</para>
<sub_segment id="idrootx2x1.segment.3.sub_segment.1">
<para id="idrootx2x1.segment.3.sub_segment.1.para.1">Text of the segment here.</para>
</sub_segment>
</segment>
</root>

這是我到目前為止擁有的XSLT:

<xsl:template match="*|@*|text()">
<xsl:copy>
    <xsl:apply-templates select="*|@*|text()"/> 
</xsl:copy> 
</xsl:template>

<xsl:template match="root">
<xsl:copy>
    <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="segment | para | sub_segment">
<xsl:copy>
    <xsl:attribute name="id">
        <xsl:value-of select="name(.)"/>.<xsl:number format="1" level="single"/>
    </xsl:attribute>
    <xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>

您可以像這樣將您的父母編號傳遞給孩子:

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

<xsl:template match="root">
<xsl:copy>
    <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="generate-id()"/>
    </xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="segment|para|sub_segment">
<xsl:param name="prev_id"/>
<xsl:copy>
    <xsl:variable name="cur_id">
        <xsl:value-of select="concat($prev_id,'.',name())"/>.<xsl:number format="1" level="single"/>
    </xsl:variable>
    <xsl:attribute name="id"><xsl:value-of select="$cur_id"/></xsl:attribute>
    <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$cur_id"/>
    </xsl:apply-templates>
</xsl:copy>
</xsl:template>

如果還有其他一些未編號的元素包裝編號的元素,請將身份模板更改為

<xsl:template match="@*|node()">
<xsl:param name="prev_id"/>
<xsl:copy>
    <xsl:apply-templates select="@*|node()">
        <xsl:with-param name="prev_id" select="$prev_id"/>
    </xsl:apply-templates>
</xsl:copy>
</xsl:template>

這樣就可以轉發父編號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM