[英]Test XSL for missing attribute
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; 这是XML样式的3种变化; I am trying to work out the presence of a value for <apsite:address>
, an empty string or a missing element. 我试图找出<apsite:address>
的值,空字符串或缺少的元素。
<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. 我需要找到其中的值为空字符串或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 ie test="not(apsite:apsite/apsite:address)"> is not working. 我的空字符串测试有效,但缺少元素的测试无效,即test =“ not(apsite:apsite / apsite:address)”>无法正常工作。
Could someone please advise what I am missing ? 有人可以告诉我我所缺少的吗?
Thanks. 谢谢。
This XSLT 1.0 stylesheet ... 这个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>'address' 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 'address' 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>'address' element was absent.</xsl:comment>
</xsl:template>
</xsl:stylesheet>
...when applied in turn to each of these 3 possible input documents... ...依次应用于这3个可能的输入文档中的每个文档时...
Test case 1: Input document: 测试用例1:输入文档:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<apsite:address>Some Address</apsite:address>
</apsite:apsite>
Test case 2: Input document: 测试案例2:输入文档:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
<apsite:address></apsite:address>
</apsite:apsite>
Test case 3: Input document: 测试案例3:输入文档:
<apsite:apsite xmlns:apsite="http://stackoverflow.com/questions/18524099">
<apsite:name>John</apsite:name>
</apsite:apsite>
...yields respectively... ...产量分别...
Test Case 1 output: 测试用例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>
Test Case 2 output: 测试案例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>
Test Case 3 output: 测试用例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.-->
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.