如果不存在,XSLT 创建元素?

XSLT create element if doesn't exist?

我正在尝试找到一种方法,仅当元素不存在时才通过 XSLT 将元素添加到输入 xml 文件。我下面的解决方案适用于元素不存在的情况,但是如果它确实存在(我仍然需要为 sessionId 赋值),它仍然会创建一个新元素。

XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="xsl exsl xs">
    <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//*[local-name() = 'SessionHeader']">
        <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
            <xsl:choose>
                <xsl:when test="not(sessionId)">
                    <sessionId><xsl:value-of select="9876543210"/></sessionId>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy><xsl:value-of select="9876543210"/></xsl:copy>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

样本XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <SessionHeader>
      <sessionId />
    </SessionHeader>
  </soap:Header>
  <soap:Body>
      ....
  </soap:Body>
</soap:Envelope>

XSLT 之后:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Header>
      <SessionHeader>
         <sessionId />
         <sessionId xmlns="">9876543210</sessionId>
      </SessionHeader>
   </soap:Header>
   <soap:Body>
       ....
   </soap:Body>
</soap:Envelope>

注意 2 个 sessionId 元素

同样,如果 sessionId 元素根本不存在,它也可以正常工作。在此先感谢您提供任何有用的帮助。

我想(或者更确切地说是猜测)你想做这样的事情:

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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="SessionHeader[not(sessionId)]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <sessionId><xsl:value-of select="9876543210"/></sessionId>
    </xsl:copy>
</xsl:template>

<xsl:template match="sessionId[not(text())]">
    <xsl:copy>
        <xsl:text>9876543210</xsl:text>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这会将值“9876543210”添加到 sessionId 元素(如果它为空)或创建一个具有上述值的新 sessionId 元素(如果它不存在)。否则默认的 identity transforn 模板将复制现有的 sessionId 元素及其现有值。

如果您可以安全地假设 sessionId 将永远是 SessionHeader 中唯一的 child,那么这里有一个简单的方法可以实现这一点:

<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="*"/>

  <!-- identity template -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="SessionHeader[not(normalize-space(sessionId))]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <sessionId>9876543210</sessionId>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

如果 SessionHeader 没有 sessionId 或 empty/all 空格 sessionId,它只是创建一个具有预定义值的空格。如果它 已经包含一个 sessionId 和一个值,那么身份模板会负责复制它。