简体   繁体   English

如果xml元素没有值,则必须输出null值

[英]null value must be printed if there is no value for the xml element

I am supposed to write an XSLT for an XML document, for which if it has no parameter for an element, the XSLT must consider a null value. 我应该为XML文档编写一个XSLT,如果它没有元素的参数,则XSLT必须考虑一个空值。 The output must be an XML document where the elements with no values in the original xml document, must be printed as <tagName>Null</tagName> 输出必须是XML文档,其中原始xml文档中没有值的元素必须打印为<tagName>Null</tagName>

This is the xml document 这是xml文件

 <salesOrderRequest> <invoiceNo>1245</invoiceNo> <PizzaType/> <Price>1099</Price> <Discount>234</Discount> </salesOrderRequest> 

This is my xslt 这是我的xslt

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XMLTransform"> <xsl:template match="/"> <xsl:for-each select="/salesOrderRequest"> <xsl:value-of select="invoiceNo"/> <xsl:value-of select="PizzaType"/> <xsl:value-of select="Price"/> <xsl:value-of select="Discount"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

 <salesOrderRequest> <invoiceNo>1245</invoiceNo> <PizzaType>Null</PizzaType> <Price>1099</Price> <Discount>234</Discount> </salesOrderRequest> 

In XSLT 2.0 or 3.0, for 在XSLT 2.0或3.0中,

try 尝试

<xsl:value-of select="if (normalize-space(PizzaType))
    then PizzaType
    else 'Null' "/>

or (if you don't want to accept <PizzaType> </PizzaType> as equivalent to <PizzaType/> ) 或(如果您不想接受<PizzaType> </PizzaType>等同于<PizzaType/>

<xsl:value-of select="if (PizzaType ne '')
    then PizzaType
    else 'Null' "/>

or 要么

<xsl:value-of select="(PizzaType[. ne ''], 'Null')[1]"/>

If you require a solution in XSLT 1.0, you should tag your question that way. 如果您需要XSLT 1.0中的解决方案,则应该以这种方式标记您的问题。

Try: 尝试:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XMLTransform">

  <xsl:template match="/">
    <xsl:apply-templates select="/salesOrderRequest/*"/>
  </xsl:template>

  <xsl:template match="salesOrderRequest/*" priority="1">
     <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="salesOrderRequest/*[.='']" priority="2">
     <xsl:text>null</xsl:text>
  </xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM