【问题标题】:XSLT 1.0 Add Elements Increment IDXSLT 1.0 添加元素增量 ID
【发布时间】:2016-02-08 11:58:25
【问题描述】:

使用 XML:

<Table>
    <Row id=1>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
    <Row id=2>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
</Table>

如何在最后添加新的 Row 元素并使用 XSLT 1.0 增加 id 属性?源 XML 可以有任意数量的 Row 元素。

<Table>
    <Row id=1>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
    <Row id=2>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
    <Row id=3>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
    <Row id=4>
       <Col1>...</Col1>
       <Col2>...</Col2>
    </Row>
</Table>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    为Table 元素编写一个模板,读出Row[last()]/@id + 1 并添加一个新的Row 或如果需要更多Rows:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
        <xsl:param name="number-of-rows-to-add" select="5"/>
    
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Table">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
                <xsl:call-template name="add-rows">
                    <xsl:with-param name="current-index" select="Row[last()]/@id + 1"/>
                    <xsl:with-param name="number-of-rows-to-add" select="$number-of-rows-to-add"/>
                </xsl:call-template>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template name="add-rows">
            <xsl:param name="current-index"/>
            <xsl:param name="number-of-rows-to-add"/>
            <Row id="{$current-index}">
                <Col1>...</Col1>
                <Col2>...</Col2>
            </Row>
            <xsl:if test="$number-of-rows-to-add > 1">
                <xsl:call-template name="add-rows">
                    <xsl:with-param name="current-index" select="$current-index + 1"/>
                    <xsl:with-param name="number-of-rows-to-add" select="$number-of-rows-to-add - 1"/>                
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:transform>
    

    在线http://xsltransform.net/3NJ38YX。

    【讨论】:

    • 感谢您的帮助。 ColX 元素会有不同的文本,但这给了我一些想法。我喜欢 xsltransform.net 链接。以前没见过那个工具。
    【解决方案2】:

    这似乎解决了我的问题。我还在看。不知道为什么,但它在 xsltransform.net 工具中不起作用。

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my" version="1.0">
    
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:variable name='last-id' select="Table/Row[last()]/@id"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Table">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
    
                <xsl:for-each select="document('')/*/my:rows/*">
                    <xsl:copy>
                        <xsl:attribute name="id"><xsl:value-of select="$last-id + position()"/></xsl:attribute>
                        <xsl:apply-templates select="@*|node()" />
                    </xsl:copy>
                </xsl:for-each>
            </xsl:copy>
        </xsl:template>
    
        <my:rows>
            <Row>
                <Col1>A</Col1>
                <Col2>B</Col2>
            </Row>
            <Row>
                <Col1>C</Col1>
                <Col2>D</Col2>
            </Row>
            <Row>
                <Col1>E</Col1>
                <Col2>F</Col2>
            </Row>
        </my:rows>
    </xsl:transform>
    

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多