0

I know that questions similar to this have been asked but I can't work out what is wrong with my test.

Here are 3 variations of how my XML could be like; I am trying to work out the presence of a value for <apsite:address>, an empty string or a missing element.

 <apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
  <apsite:name>John</apsite:name>
  <apsite:address>Some Address</apsite:name>
 </apsite:apsite>

<apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
  <apsite:name>John</apsite:name>
  <apsite:address></apsite:name>
 </apsite:apsite>

<apsite:apsite xmlns="http://www.w3.org/1999/xhtml" >
  <apsite:name>John</apsite:name>
</apsite:apsite>

I need to find the instances where the value of is an empty string or null. These are my if conditions.

                              <xsl:if  
                test="../apsite:address = ''">
                <dc:source>
                    <xsl:value-of select="." />
                </dc:source>
            </xsl:if>
            <xsl:if
                test="not(apsite:apsite/apsite:address)">
                <dc:source>
                    <xsl:value-of select="." />
                </dc:source>
            </xsl:if>
            <xsl:if
                test="../apsite:adress != ''">
                <dc:source>
                    <xsl:value-of select="../apsite:oldAddress" />
                </dc:source>
            </xsl:if>

My test for the empty string works but not for the missing element i.e test="not(apsite:apsite/apsite:address)"> is not working.

Could someone please advise what I am missing ?

Thanks.

4

1 回答 1

1

这个 XSLT 1.0 样式表...

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:apsite="http://stackoverflow.com/questions/18524099" 
  xmlns:dc="some-other-url" >
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="apsite:address[.='']">
  <dc:source>
    <xsl:value-of select="'New value'" />
  </dc:source>
  <xsl:comment>&apos;address&apos; element was empty.</xsl:comment>
</xsl:template>

<xsl:template match="apsite:address[not(.='')]">
  <dc:source>
    <xsl:value-of select="." />
  </dc:source>
  <xsl:comment>Text of &apos;address&apos; element was copied.</xsl:comment>
</xsl:template>

<xsl:template match="apsite:apsite[not(apsite:address)]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <dc:source>
      <xsl:value-of select="'New value'" />
    </dc:source>
  </xsl:copy>
  <xsl:comment>&apos;address&apos; element was absent.</xsl:comment>
</xsl:template>


</xsl:stylesheet>

...当依次应用于这 3 个可能的输入文档中的每一个时...

测试用例1:输入文档:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
  <apsite:address>Some Address</apsite:address>
</apsite:apsite>

测试用例 2:输入文档:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
  <apsite:address></apsite:address>
</apsite:apsite>

测试用例 3:输入文档:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
</apsite:apsite>

...产量分别...

测试用例 1 输出:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
  <dc:source xmlns:dc="some-other-url">Some Address</dc:source>
  <!--Text of 'address' element was copied.-->
</apsite:apsite>

测试用例 2 输出:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
  <dc:source xmlns:dc="some-other-url">New value</dc:source>
  <!--'address' element was empty.-->
</apsite:apsite>

测试用例 3 输出:

<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
  <apsite:name>John</apsite:name>
  <dc:source xmlns:dc="some-other-url">New value</dc:source>
</apsite:apsite>
<!--'address' element was absent.-->

我希望这有帮助。

于 2013-08-30T04:25:19.937 回答