XSLT Run Template внутри Template, который добавляет элемент Root - PullRequest
0 голосов
/ 11 февраля 2020

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, если я НЕ добавляю элемент root с другим шаблоном, но я не могу понять, как это сделать, если это необходимо для запуска внутри другого шаблона.

Я также знаю, что мог бы сделать это с 2 последовательными преобразованиями, но в моем случае это не вариант.

1 Ответ

1 голос
/ 11 февраля 2020

Если вы хотите изменить узел 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.

...