简体   繁体   English

XSLT1.0在XML中查找未知元素并将其放置在其他位置

[英]XSLT1.0 find unknown elements in XML and put them elsewhere

Im not sure if the headline is correct. 我不确定标题是否正确。 I have a XML-file looking like this: 我有一个看起来像这样的XML文件:

XML-FILE XML文件

<main>
    <mainelement attribute="on">
        <basic>23</basic>
        random
        <stuff id="10"/>
        <a>sometext</a>
        <b_1>1</b_1>
        <b_2>0.300000</b_2>
        <d att="one"/>
        <e>value</e>
    </mainelement>
    <otherlement>
    ...
    </otherlement>
</main>

I format the file to fit a certain norm. 我格式化文件以符合特定规范。 Tags ae are all known and wanted. 标签ae都是已知的和通缉的。 but there is a possibility that other stuff is in there as well which doesnt match the norm. 但也可能存在其他与规范不符的内容。 (ie basic, random and stuff). (即基本,随机和内容)。 I wrote the following 我写了以下

XSL-Script XSL脚本

<xsl:template match="main/mainelement">
        <mainelement>
            <xsl:copy-of select="a">
            <xsl:variable name="b1" select="b_1"/>
            <xsl:variable name="b2" select="b_2"/>
            <b item1="{b1}" item2="{b2}">
            <xsl:variable name="c" select="c"/>
            <xsl:if test="$c">
                <xsl:copy-of select="c">
            </xsl:if>
            <xsl:if test="not(c)">
                <c>default</c>
            </xsl:if>
            <xsl:copy-of select="d">
            <xsl:variable name="e" select="e"/>
            <xsl:element name="e">
                <xsl:attribute name="att">
                    <xsl:value-of select="$e">
                </xsl:attribute>
            </xsl:element>
        </mainelement>

        <!-- surrounds every element or text() that is unknown with the undefined-tag -->
        <xsl:for-each select="* | text()">
            <xsl:choose>
                <xsl:when test="name()='a'"/>
                <xsl:when test="name()='b_1'"/>
                <xsl:when test="name()='b_2'"/>
                <xsl:when test="name()='c'"/>
                <xsl:when test="name()='d'"/>
                <xsl:otherwise>
                    <undefined>
                        <xsl:copy-of select="current()"/>
                    </undefined>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

that does almost what i want to achieve. 那几乎是我想要实现的目标。 The problem is, I dont know if there is anything in there beside the norm, and if nothing like basic, random or stuff is in the above, there should be no < undefinded > tag at all. 问题是,我不知道规范之外是否有任何东西,并且如果上面没有像基本,随机或东西之类的东西,那么根本就应该没有<unfinded>标签。 the output is 输出是

OUTPUT 输出值

<main>
    <mainelement attribute="on">
        <a>sometext</a>
        <b item1="1" item2="0.3">
        <c>default</c>
        <d att="one"/>
        <e att="value"/>
    </mainelement>
    <undefined>
        <basic>23</basic>
    </undefined>
    <undefined>
        random
    </undefined>
    <undefined>
        <stuff id="10"/>
    </undefined>
    <otherlement>
    ...
    </otherlement>
</main>

but i want to achieve this: 但我想实现这一目标:

Desired-Output 期望的输出

<main>
    <mainelement attribute="on">
        <a>sometext</a>
        <b item1="1" item2="0.3">
        <c>default</c>
        <d att="one"/>
        <e att="value"/>
    </mainelement>
    <undefined>
        <basic>23</basic>
        random
        <stuff id="10"/>
    </undefined>
    <otherlement>
    ...
    </otherlement>
</main>

i guess i overthought that too much, and so i got a bit blided for an easy solution, so every complete different atempt is apreciated too. 我想我想得太多了,所以我为一个简单的解决方案而有些失落,所以每一个完全不同的尝试也都感到高兴。 A better solution for the xsl:choose method would be cool too, its style creeps me off... the important part is, that i want to exclude everything that doesnt match the norm, and put it somewhere else. xsl:choose方法的一个更好的解决方案也很酷,它的风格使我无所适从……重要的是,我要排除不符合规范的所有内容,并将其放在其他位置。 But if there is nothing beside the norm, nothing should happen, except my altering of ae I thought of concating variables in the xsl:choose and only create an output if the whole variable was not empty, but that seemed not to be possible in XSL1.0 但是,如果除了规范之外什么也没有,那么什么都不会发生,除了我对ae的修改之外,我想到了在xsl:choose中隐藏变量,并且仅在整个变量不为空的情况下才创建输出,但是在XSL1中似乎无法实现.0

hope i explained my problem properly. 希望我能正确解释我的问题。 Lots of thanks in advance! 提前非常感谢!

reineke 雷内克

I am mostly guessing here, but perhaps this is what you're looking for: 我主要是在这里猜测,但这也许是您要寻找的:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>
        <undefined>
            <xsl:copy-of select="mainelement/node()[not(self::a or self::b_1 or self::b_2 or self::c or self::d or self::e)]"/>
        </undefined>
        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="mainelement">
    <xsl:copy>
        <xsl:copy-of select="@* | a"/>
        <b item1="{b_1}" item2="{b_2}"/>
        <xsl:copy-of select="c"/>
        <xsl:if test="not(c)">
            <c>default</c>
        </xsl:if>
        <xsl:copy-of select="d"/>
        <e att="{e}"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Applied to your example input, the result is: 应用于示例输入, 结果为:

<?xml version="1.0" encoding="UTF-8"?>
<main>
   <mainelement attribute="on">
      <a>sometext</a>
      <b item1="1" item2="0.300000"/>
      <c>default</c>
      <d att="one"/>
      <e att="value"/>
   </mainelement>
   <undefined>
      <basic>23</basic>
        random
        <stuff id="10"/>
   </undefined>
   <otherlement>
    ...
    </otherlement>
</main>

Alternatively, you could do a slightly more elegant: 另外,您可以做些更优雅的事情:

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>

        <xsl:variable name="whitelist" select="mainelement/a | mainelement/b_1 | mainelement/b_2 | mainelement/c | mainelement/d | mainelement/e " />
        <undefined>
            <xsl:copy-of select="mainelement/node()[not(count(.|$whitelist) = count($whitelist))]"/>
        </undefined>

        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

or, if your processor supports the EXSLT set:difference() extension function: 或者,如果您的处理器支持EXSLT set:difference()扩展功能:

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>

        <xsl:variable name="whitelist" select="mainelement/a | mainelement/b_1 | mainelement/b_2 | mainelement/c | mainelement/d | mainelement/e " />
        <undefined>
            <xsl:copy-of select="set:difference(mainelement/node(), $whitelist)"/>
        </undefined>

        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

Edit: 编辑:

it almost does what i want, except the following: If there is noting undefined (ie no basic, random, stuff) in the mainelement, there should be no "undefined" tag at all. 它几乎可以满足我的要求,但以下情况除外:如果维护元素中没有未定义的(即没有基本的,随机的东西),则根本没有“未定义的”标签。

Well, then put the "undefined" nodes in a variable first, then create the <undefined> element only if there's something in that variable: 好吧,然后首先将“未定义”节点放入变量中,然后仅在该变量中包含某些内容时创建<undefined>元素:

<xsl:variable name="undefined" select="mainelement/node()[not(self::a or self::b_1 or self::b_2 or self::c or self::d or self::e)]"/>
<xsl:if test="$undefined">
    <undefined>
        <xsl:copy-of select="$undefined"/>
    </undefined>
</xsl:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM