[英]XSLT1.0 find unknown elements in XML and put them elsewhere
我不确定标题是否正确。 我有一个看起来像这样的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>
我格式化文件以符合特定规范。 标签ae都是已知的和通缉的。 但也可能存在其他与规范不符的内容。 (即基本,随机和内容)。 我写了以下
<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>
那几乎是我想要实现的目标。 问题是,我不知道规范之外是否有任何东西,并且如果上面没有像基本,随机或东西之类的东西,那么根本就应该没有<unfinded>标签。 输出是
<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>
但我想实现这一目标:
<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>
我想我想得太多了,所以我为一个简单的解决方案而有些失落,所以每一个完全不同的尝试也都感到高兴。 xsl:choose方法的一个更好的解决方案也很酷,它的风格使我无所适从……重要的是,我要排除不符合规范的所有内容,并将其放在其他位置。 但是,如果除了规范之外什么也没有,那么什么都不会发生,除了我对ae的修改之外,我想到了在xsl:choose中隐藏变量,并且仅在整个变量不为空的情况下才创建输出,但是在XSL1中似乎无法实现.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>
应用于示例输入, 结果为:
<?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>
另外,您可以做些更优雅的事情:
<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>
或者,如果您的处理器支持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>
它几乎可以满足我的要求,但以下情况除外:如果维护元素中没有未定义的(即没有基本的,随机的东西),则根本没有“未定义的”标签。
好吧,然后首先将“未定义”节点放入变量中,然后仅在该变量中包含某些内容时创建<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.