TL;DR:這是我的小提琴:https ://xsltfiddle.liberty-development.net/6qtiBn6/2
擴展我之前的 XSLT 1.0(Microsoft 供應商)問題的答案,實際上我還需要從一個節點中創建一些重復欄位,這些欄位的值由分隔符“|”分隔。我確實收到了一個引數欄位count,告訴我要制作的那個節點的副本數。節點是one_two。
<?xml version="1.0" encoding="UTF-8"?>
<data>
<settings>
<field style="element" level="count" target="param">3</field>
<field style="element" level="one" target="one"></field>
<field style="attribute" level="one.quality" target="one.quality">high</field>
<field style="attribute" level="one.weight" target="one.weight">10 kg</field>
<field style="element" level="one_two" target="two"></field>
<field style="attribute" level="one_two.type" target="two.type">a|b|c</field>
<field style="element" level="one_two_ten" target="ten">alpha|beta|gamma</field>
<field style="attribute" level="one_two_ten.type" target="ten.type">apple|ball|NULL</field>
<field style="attribute" level="one_two_ten.age" target="ten.age">baby|young|old</field>
<field style="element" level="one_three" target="three"></field>
<field style="attribute" level="one_three.color" target="three.color">black</field>
<field style="element" level="one_three_four" target="four" >B</field>
<field style="attribute" level="one_three_four.length" target="four.length">12 cm</field>
<field style="attribute" level="one_three_four.width" target="four.width"> 7 cm</field>
<field style="element" level="one_three_five" target="five" >C</field>
<field style="element" level="one_six" target="six" ></field>
<field style="attribute" level="one_six.size" target="six.size" >large</field>
<field style="element" level="one_six_seven" target="seven" ></field>
<field style="element" level="one_six_seven_eight" target="eight">D</field>
<field style="element" level="one_nine" target="nine">E</field>
</settings>
</data>
這是我擁有的 XSLT 1.0 代碼:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.example.org/standards/template/1" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="elem" match="field[@style='element']" use="substring-before(@level, @target)" />
<xsl:key name="attr" match="field[@style='attribute']" use="substring-before(@level, '.')" />
<xsl:variable name="maxcount" select="//field[@level = 'count']"/>
<xsl:template match="/">
<template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/standards/template.xsd" xmlns:ac="http://www.example.org/Standards/abc/1" Version="2022-01">
<xsl:apply-templates select="key('elem', '')" />
</template>
</xsl:template>
<xsl:template match="field[@style='element']">
<xsl:choose>
<xsl:when test="@level = 'one_two'">
<xsl:call-template name="multiply">
<xsl:with-param name="maxCount" select="$maxcount" />
<xsl:with-param name="target" select="@target" />
<xsl:with-param name="level" select="@level" />
</xsl:call-template>
</xsl:when>
<xsl:when test="@target != 'param'">
<xsl:element name="{@target}">
<xsl:apply-templates select="key('attr', @level)" />
<xsl:value-of select="." />
<xsl:apply-templates select="key('elem', concat(@level, '_'))" />
</xsl:element>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:template>
<xsl:template match="field[@style='attribute']">
<xsl:attribute name="{substring-after(@target, '.')}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<xsl:template name="multiply">
<xsl:param name="maxCount" />
<xsl:param name="target" />
<xsl:param name="level" />
<xsl:param name="i" select="1" />
<xsl:choose>
<xsl:when test="$i <= $maxCount">
<xsl:element name="{$target}">
<xsl:apply-templates select="key('attr', @level)" />
<xsl:value-of select="." />
<xsl:apply-templates select="key('elem', concat(@level, '_'))" />
</xsl:element>
<xsl:call-template name="multiply">
<xsl:with-param name="maxCount" select="$maxCount" />
<xsl:with-param name="target" select="@target" />
<xsl:with-param name="level" select="@level" />
<xsl:with-param name="i" select="$i 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
這是我從上面得到的結果:
<?xml version="1.0" encoding="UTF-8"?>
<template xmlns="http://www.example.org/standards/template/1"
xmlns:ac="http://www.example.org/Standards/abc/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/standards/template.xsd"
Version="2022-01">
<one quality="high" weight="10 kg">
<two type="a|b|c">
<ten type="apple|ball|NULL" age="baby|young|old">alpha|beta|gamma</ten>
</two>
<two type="a|b|c">
<ten type="apple|ball|NULL" age="baby|young|old">alpha|beta|gamma</ten>
</two>
<two type="a|b|c">
<ten type="apple|ball|NULL" age="baby|young|old">alpha|beta|gamma</ten>
</two>
<three color="black">
<four length="12 cm" width=" 7 cm">B</four>
<five>C</five>
</three>
<six size="large">
<seven>
<eight>D</eight>
</seven>
</six>
<nine>E</nine>
</one>
</template>
我還沒有完全到那里。即這就是我需要的:注意如何one_two擴展為 3 個元素 ie count) 并且子項從分隔串列中分離出相應的值。此外,NULL 關鍵字意味著第二個重復的屬性將被跳過(所以我可能想在那里應用替換 - 待定)。
<?xml version="1.0" encoding="UTF-8"?>
<template xmlns="http://www.example.org/standards/template/1"
xmlns:ac="http://www.example.org/Standards/abc/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/standards/template.xsd"
Version="2022-01">
<one quality="high" weight="10 kg">
<two type="a">
<ten type="apple" age="baby">alpha</ten>
</two>
<two type="b">
<ten type="ball" age="young">beta</ten>
</two>
<two type="c">
<ten age="old">gamma</ten>
</two>
<three color="black">
<four length="12 cm" width=" 7 cm">B</four>
<five>C</five>
</three>
<six size="large">
<seven>
<eight>D</eight>
</seven>
</six>
<nine>E</nine>
</one>
</template>
我在這里只需要再做兩件事:(1)我需要修改multiply模板,以便每個相乘的元素substring在 $1 = 1 時選擇計數器 $iie 的相對部分,我們選擇第一部分,等等。考慮如何choose在回圈中為 counter應用 a i,然后選擇相應專案的子字串。(2) 注意ten元素(在重復元素下<two type="c">)沒有型別,因為它在提供的字串中為 NULL。我想我需要一種 IF 陳述句。
uj5u.com熱心網友回復:
您輸入的格式非常不方便。
此外,您添加的要求中有一些非常奇怪的東西:根據您的描述,元素:
<field style="element" level="count" target="param">3</field>
表示輸入中的某些元素需要在輸出中復制 3 次 - 但它沒有指定哪一個。您告訴我們該指令是指:
<field style="element" level="one_two" target="two"></field>
及其后代節點 - 但此資訊不包含在輸入本身中(也許它可能源于以下事實:后代節點的字串值是分隔字串,每個字串有 3 個標記,但這需要一個明顯不同的程序) .
無論如何,利用這些外部知識,我認為您可以將上一個問題的答案調整為:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.example.org/standards/template/1" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="elem" match="field[@style='element'][@level!='count']" use="substring-before(@level, @target)" />
<xsl:key name="attr" match="field[@style='attribute']" use="substring-before(@level, '.')" />
<xsl:template match="/">
<template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/standards/template.xsd" xmlns:ac="http://www.example.org/Standards/abc/1" Version="2022-01">
<xsl:apply-templates select="key('elem', '')" />
</template>
</xsl:template>
<!-- create element -->
<xsl:template match="field[@style='element']">
<xsl:param name="N" select="1"/>
<xsl:element name="{@target}">
<!-- add attributes -->
<xsl:apply-templates select="key('attr', @level)" >
<xsl:with-param name="N" select="$N"/>
</xsl:apply-templates>
<!-- add text node -->
<xsl:call-template name="get-Nth-token">
<xsl:with-param name="N" select="$N"/>
<xsl:with-param name="list" select="."/>
</xsl:call-template>
<!-- add child elements -->
<xsl:apply-templates select="key('elem', concat(@level, '_'))" >
<xsl:with-param name="N" select="$N"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<!-- create attribute -->
<xsl:template match="field[@style='attribute']">
<xsl:param name="N" select="1"/>
<xsl:attribute name="{substring-after(@target, '.')}">
<!-- string-value -->
<xsl:call-template name="get-Nth-token">
<xsl:with-param name="N" select="$N"/>
<xsl:with-param name="list" select="."/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<!-- handle "special" element -->
<xsl:template match="field[@level='one_two']" name="special" priority="1">
<xsl:param name="count" select="../field[@level='count']"/>
<xsl:if test="$count > 0">
<xsl:call-template name="special">
<xsl:with-param name="count" select="$count - 1"/>
</xsl:call-template>
<two>
<xsl:apply-templates select="key('attr', @level) | key('elem', concat(@level, '_'))" >
<xsl:with-param name="N" select="$count"/>
</xsl:apply-templates>
</two>
</xsl:if>
</xsl:template>
<!-- get Nth token -->
<xsl:template name="get-Nth-token">
<xsl:param name="list"/>
<xsl:param name="N"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:choose>
<xsl:when test="$N = 1">
<xsl:value-of select="substring-before(concat($list, $delimiter), $delimiter)"/>
</xsl:when>
<xsl:when test="contains($list, $delimiter) and $N > 1">
<!-- recursive call -->
<xsl:call-template name="get-Nth-token">
<xsl:with-param name="list" select="substring-after($list, $delimiter)"/>
<xsl:with-param name="N" select="$N - 1"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
應用于您的輸入示例,這將產生:
結果
<?xml version="1.0" encoding="UTF-8"?>
<template xmlns="http://www.example.org/standards/template/1"
xmlns:ac="http://www.example.org/Standards/abc/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/standards/template.xsd"
Version="2022-01">
<one quality="high" weight="10 kg">
<two type="a">
<ten type="apple" age="baby">alpha</ten>
</two>
<two type="b">
<ten type="ball" age="young">beta</ten>
</two>
<two type="c">
<ten type="NULL" age="old">gamma</ten>
</two>
<three color="black">
<four length="12 cm" width=" 7 cm">B</four>
<five>C</five>
</three>
<six size="large">
<seven>
<eight>D</eight>
</seven>
</six>
<nine>E</nine>
</one>
</template>
我相信這是預期的結果,只有一個小例外。
為避免創建字串值為 的屬性或元素,"NULL"請先將回傳的標記放在變數中;然后僅在 時創建節點$token != 'NULL'。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474963.html
上一篇:使用XSLT洗掉XML中的父節點
