這是當前的 XML:
<include>
...
<data>
<id>10001</id>
<name>Soup</name>
<minorder>1</minorder>
<maxorder>5</maxorder>
<minprice>30</minprice>
<maxprice>60</maxprice>
<canSell>1</canSell>
<isSale>1</isSale>
</data>
<data>
<id>10002</id>
<name>Noodle</name>
<minorder>1</minorder>
<saleprice>45</saleprice>
<maxorder>3</maxorder>
<minprice>30</minprice>
<maxprice>60</maxprice>
<canSell>1</canSell>
<isSale>0</isSale>
</data>
<data>
<id>10003</id>
<name>Shrimp</name>
<minorder>1</minorder>
<maxorder>5</maxorder>
<minprice>30</minprice>
<maxprice>60</maxprice>
<canSell>0</canSell>
<saleprice>45</saleprice>
<isSale>1</isSale>
</data>
...
</include>
我想把整個移到<saleprice>上面<minprice>。在<saleprice>不存在的情況下,我們將使用<maxprice>/ 2的值創建它。
這是我撰寫的 XSL 代碼:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="include/data">
<xsl:copy>
<xsl:apply-templates/>
<xsl:if test="not(saleprice)">
<saleprice>
<xsl:value-of select="format-number(maxprice div 2,'0.#######')"/>
</saleprice>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但是我現在得到的結果只是創建元素,<saleprice>如果它不存在的話。
我將非常感謝您提供的建議。
uj5u.com熱心網友回復:
考慮擴展<xsl:apply-templates>對子節點顯式排序的呼叫,并saleprice在單獨的模板中處理缺失/非缺失:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes" omit-xml-declaration="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM TEMPLATE -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- date WITHOUT saleprice -->
<xsl:template match="data[not(saleprice)]">
<xsl:copy>
<xsl:apply-templates select="id|name"/>
<!-- Some other elements -->
<saleprice>
<xsl:value-of select="format-number(maxprice div 2,'0.#######')"/>
</saleprice>
<xsl:apply-templates select="minprice|maxprice"/>
<!-- Some other elements -->
</xsl:copy>
</xsl:template>
<!-- date WITH saleprice -->
<xsl:template match="data[saleprice]">
<xsl:copy>
<xsl:apply-templates select="id|name"/>
<!-- Some other elements -->
<xsl:apply-templates select="saleprice"/>
<xsl:apply-templates select="minprice|maxprice"/>
<!-- Some other elements -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
uj5u.com熱心網友回復:
我會讓minprice元素觸發插入,例如
<xsl:template match="minprice[not(../saleprice)]">
<saleprice>
<xsl:value-of select="format-number(../maxprice div 2,'0.#######')"/>
</saleprice>
<xsl:next-match/>
</xsl:template>
和運動:
<xsl:template match="minprice[../saleprice]">
<xsl:copy-of select="../saleprice"/>
<xsl:next-match/>
</xsl:template>
然后添加<xsl:template match="data/saleprice"/>以防止身份轉換模板正常復制
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
你就完成了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/406581.html
標籤:
上一篇:條件陣列串列?
