添加根元素的模板内的 XSLT 运行 模板

XSLT Run Template inside Template that adds Root element

XML 文件:

<Item isNew="1">
    <project_number>00123</project_number>
    <name>Copy Stuff</name>
    <owned_by_id>D9CB2DAFA027466490E50FBEF05E17E9</owned_by_id>
</Item>

预期输出:

<NEW>
    <Item isNew="1">
        <project_number>00123</project_number>
        <name>Copy of PDP Template</name>
        <owned_by_id>D9CB2DAFA027466490E50FBEF05E17E9</owned_by_id>
        <new_classification>Test</new_classification>
        <new_sales_id>9876</new_sales_id>
        <new_sales_type>OEM</new_sales_type>
        <new_product_line />
    </Item>
</NEW>

XSL 样式表(产生不正确的输出):

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:cs="urn:cs"
        exclude-result-prefixes="msxsl cs">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/">
        <xsl:element name="NEW">
            <xsl:call-template name="InnerTemplate" />
        </xsl:element>
    </xsl:template>

    <xsl:template name="InnerTemplate">
        <xsl:param name="DocumentToAdd">
            <new_classification>Test</new_classification>
            <new_sales_id>9876</new_sales_id>
            <new_sales_type>OEM</new_sales_type>
            <new_product_line />
        </xsl:param>

        <xsl:copy-of select="."/>
        <xsl:copy-of select="$DocumentToAdd"/>
    </xsl:template>
</xsl:stylesheet>

输出不正确:

<NEW>
  <Item isNew="1">
    <project_number>00123</project_number>
    <name>Copy Stuff</name>
    <owned_by_id>D9CB2DAFA027466490E50FBEF05E17E9</owned_by_id>
  </Item>
  <new_classification>Test</new_classification>
  <new_sales_id>9876</new_sales_id>
  <new_sales_type>OEM</new_sales_type>
  <new_product_line />
</NEW>

我仅限于 XSLT 1.0

如果我不使用另一个模板添加根元素,我知道如何使用模板将新节点放入 Item,但我似乎无法弄清楚该怎么做如果它需要 运行 在另一个模板中。

我也知道我可以用 2 个顺序转换来做到这一点,但在我的情况下这不是一个选项。

如果要更改 Item 节点,请使用与之匹配的模板。试试这样吧。

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:cs="urn:cs"
    exclude-result-prefixes="msxsl cs">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:param name="DocumentToAdd">
        <new_classification>Test</new_classification>
        <new_sales_id>9876</new_sales_id>
        <new_sales_type>OEM</new_sales_type>
        <new_product_line />
    </xsl:param>

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

    <xsl:template match="/">
        <NEW>
            <xsl:apply-templates/>
        </NEW>
    </xsl:template>

    <!-- template match for Item -->
    <xsl:template match="Item">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
            <!-- add elements inside this node  -->
            <xsl:copy-of select="$DocumentToAdd"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

查看实际效果:http://xsltransform.net/6qjwabD