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

时间:2015-02-02 19:03:54

标签: xml xslt

我试图找到一种方法来添加一个元素来通过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元素根本不存在,它可以正常工作。提前感谢任何有用的帮助。

2 个答案:

答案 0 :(得分:1)

我认为(或者说猜测)你想做的事情如下:

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元素。否则,默认的身份转换模板将复制现有的sessionId元素及其现有值。

答案 1 :(得分:0)

如果你可以放心地认为sessionId永远是SessionHeader的唯一孩子,那么这是一个简单的方法来实现这个目标:

<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或空/全空白sessionId,则只需创建一个具有预定义值的空格。如果 已经包含带有值的sessionId,那么身份模板会负责复制它。

相关问题