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

Templates 在添加根元素的模板内运行XSLT模板,templates,xslt,xslt-1.0,Templates,Xslt,Xslt 1.0,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"

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

00123
抄袭材料
D9CB2DAFA027466490E50FBEF05E17E9
预期输出:

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

00123
PDP模板的副本
D9CB2DAFA027466490E50FBEF05E17E9
测验
9876
原始设备制造商
XSL样式表(产生不正确的输出):

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

测验
9876
原始设备制造商
输出不正确:

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

00123
抄袭材料
D9CB2DAFA027466490E50FBEF05E17E9
测验
9876
原始设备制造商
我仅限于XSLT1.0

如果我没有用另一个模板添加根元素,我知道如何使用模板将新节点放入
项中,但如果它需要在另一个模板中运行,我似乎不知道如何执行此操作


我也知道我可以用两个顺序转换来完成这项工作,但在我的例子中,这不是一个选项。

如果您想更改
节点,请使用与之匹配的模板。试着这样做

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

测验
9876
原始设备制造商
查看它的实际操作: