根据预期结果,在XSLT代码中需要帮助形成子节点

时间:2017-12-05 07:07:00

标签: xslt

在形成3岁和4岁儿童元素时遇到困难,如下面的预期输出,但没有运气。

来自外部来源的输入消息:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <PurchaseOrder id="aoi00037607">
        <attr attr-name="A">
            <new-value>adi00010210</new-value>
        </attr>
        <attr attr-name="B">
            <new-value>99</new-value>
        </attr>
        <attr attr-name="C">
            <new-value>active</new-value>
        </attr>
        <attr attr-name="D">
            <new-value>
                <child1>iop00010538</child1>
                <child2>2</child2>
            </new-value>
            <new-value>
                <child1>cid2313213</child1>
                <child2>2</child2>
            </new-value>
            <new-value>
                <child1>hri00075562</child1>
                <child2>1</child2>
            </new-value>
        </attr>
        <attr attr-name="E">
            <new-value>
                <materials type="required">
                    <material system="XXX">
                        <attr attr-name="child3">primary</attr>
                        <attr attr-name="child4">2</attr>
                    </material >
                </materials>
            </new-value>
        </attr>
</PurchaseOrder>

写入XSLT代码以进行转换

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="urn:demo:PurchaseOrder">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:key name="keyAttrName" match="attr" use="@attr-name" />
    <xsl:template match="PurchaseOrder">
        <ns0:PurchaseOrderMSG>>
            <Orders>
                <Order id="{@id}">
                    <xsl:for-each select="attr[generate-id() = generate-id(key('keyAttrName', @attr-name)[1])]">
                        <xsl:variable name="nodeName" select="@attr-name" />
                        <xsl:choose>
                            <xsl:when test="key('keyAttrName', @attr-name)/new-value/*/node()">
                                <xsl:for-each select="new-value">
                                    <xsl:element name="{$nodeName}">
                                        <xsl:copy-of select="*" />
                                    </xsl:element>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:when test="key('keyAttrName', @attr-name)/new-value/materials/material">
                                <xsl:for-each select="key('keyAttrName', @attr-name)">
                                    <xsl:element name="{$nodeName}">
                                        <xsl:copy-of select="attr" />
                                    </xsl:element>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:for-each select="key('keyAttrName', @attr-name)">
                                    <xsl:element name="{$nodeName}">
                                        <xsl:value-of select="new-value" />
                                    </xsl:element>
                                </xsl:for-each>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </Order>
            </Orders>
        </ns0:PurchaseOrderMSG>
    </xsl:template>
</xsl:stylesheet>

以上xslt中的以下代码未给出任何结果

                    <xsl:when test="key('keyAttrName', @attr-name)/new-value/materials/material">
                        <xsl:for-each select="key('keyAttrName', @attr-name)">
                            <xsl:element name="{$nodeName}">
                                <xsl:copy-of select="attr" />
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:when>

预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:PurchaseOrderMSG xmlns:ns0="urn:demo:PurchaseOrder">
    <Orders>
        <Order>
            <A>adi00010210</A>
            <B>99</B>
            <C>active </C>
            <D>
                <Child1>iop00010538</Child1>
                <Child2>2</Child2>
            </D>
            <D>
                <child1>cid2313213</child1>
                <child2>2</child2>
            </D>
            <D>
                <child1>hri00075562</child1>
                <child2>1</child2>
            </D>
            <E>
                <Materials type="required">
                    <Material system="XXXX">
                        <child3>primary</child3>
                        <child4>2</child4>
                    </Material>
                </Materials>
            </E>
        </Order>
    </Orders>
</ns0:PurchaseOrderMSG>

需要纠正哪些才能获得预期结果?

参考链接:Need help in forming target xml using XSLT

1 个答案:

答案 0 :(得分:1)

更改

<xsl:when test="key('keyAttrName', @attr-name)/new-value/*/node()">

<xsl:when test="key('keyAttrName', @attr-name)/new-value/*/node()[not(name() = 'material')]">

因为此终止条件而您的指定脚本未处理。