[英]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.