创建一个元素,如果它不存在然后向上移动到另一个元素

Create an element if it doesn't exist then move up on an other element

这是当前的 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> 如果它不存在。

我将非常感谢您提出的建议。

我会让 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>

你已经完成了,即整个代码是

<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="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <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:stylesheet>

考虑扩展 <xsl:apply-templates> 调用以明确排序子节点并在单独的模板中处理 missing/non-missing 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>