0

私はコードから始めます-私たちは皆コードが好きです:D

XML:

    <report>
        <subject>
            <subjectId>1</subjectId>
            <name>John</name>
            <surname>Doe</surname>
        </subject>
        <subject>
            <subjectId>2</subjectId>
            <name>Frank</name>
            <surname>Timothy</surname>
        </subject>
        <individual>
            <individualId>10</individualId>
            <name>Isaac</name>
            <surname>Newton</surname>
            <co-worker>
                <subject>
                    <subjectId>1</subjectId>
                    <inXml>true</inXml>
                </subject>
                <subject>
                    <subjectId>2</subjectId>
                    <inXml>true</inXml>
                </subject>
            </co-worker>
        </individual>

        <owner>
            <subject>
                <subjectId>2</subjectId>
                <inXml>true</inXml>
            </subject>
            <share>100</share>
        </owner>

        <individual>
            <individualId>10</individualId>
            <inXml>true</inXml>
        </individual>
    </report>

XML 2:

    <report>
        <owner>
            <individual>
                <individualId>10</individualId>
                <inXml>true</inXml>
            </individual>
        </owner>
        <individual>
            <individualId>2</individualId>
            <name>John</name>
            <surname>Doe</surname>
            <co-worker>
                <individual>
                    <individualId>10</individualId>
                    <inXml>true</inXml>
                </individual>
            </co-worker>
        </individual>
        <individual>
            <individualId>10</individualId>
            <name>Isaac</name>
            <surname>Newton</surname>
            <co-worker>
                <individual>
                    <individualId>2</individualId>
                    <inXml>true</inXml>
                </individual>
            </co-worker>
        </individual>

    </report>

XSLT:

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

        <xsl:template match="individual[inXml='true']">
            <xsl:variable name="indId" select="./individualId/text()" />
            <xsl:variable name="result" select="//individual[not(inXml) and individualId=$indId]/*" />
            <xsl:choose>
                <xsl:when test="$result != ''">
                    <xsl:copy>
                        <xsl:apply-templates select="$result" />
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="." />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

        <xsl:template match="subject[inXml='true']">
            <xsl:variable name="subId" select="./subjectId/text()" />
            <xsl:variable name="result" select="//subject[not(inXml) and subjectId=$subId]/*" />
            <xsl:choose>
                <xsl:when test="$result != ''">
                    <xsl:copy>
                        <xsl:apply-templates select="$result" />
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="." />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

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

    </xsl:stylesheet>

私は何を達成しようとしていますか?-「inXml」タグが表示されている場所に件名/個人をコピーしたい。XSLTは機能しているようですが、少し大きいxmlの場合は約1MBです(大きくはありません)。私のJavaアプリケーションはjava.lang.OutOfMemoryError:Javaヒープスペースで失敗します。ストリームをファイルにリダイレクトしました。驚くべきことに、変換の結果を含むファイルは、光速で成長していました。15秒後に約300MBになりました。:D hehehe-これは、私のxlstに何らかのエラーがあり、無限ループが発生することを証明しています。

重要なのは、ノードのコピー中に、すでに「inXml」が内部にある場合があることです。そのため、テンプレートまたは結果を適用しています。私が準備したXMLは、問題を表しています。

編集:XML2は私のアプリケーションを完全に台無しにする可能性が非常に高いです。どうすれば解決できますか?XSLTによるjaxbのアンマーシャリングの問題を修正したいと思っていましたが、これは私が知っている解決策ではありません。アンマーシャリングプロセス中に、XMLノードを追加して挿入するのではなく、オブジェクトの参照を挿入する必要があります。どうすればこれを達成できますか?

前もって感謝します!

4

1 に答える 1

0

データに循環がある場合、コードは無限再帰に入ります。

これを防ぐための条件not(inXml)であるべきだと思います。not(inXML='true')しかし、あなたのデータを知らないので、確信が持てません。

于 2012-09-28T11:53:52.633 に答える