我努力測試,看看是否存在一個元素。如果沒有,我想添加一個默認值。這裏是我的XML:變換後,xslt添加默認值,其中xml元素不存在
<Transaction>
<InvoicePeriod>1</InvoicePeriod>
<Product>Shoe</Product>
</Transaction>
如果InvoicePeriod元素犯規存在,默認的應該是1:
<Transaction>
<Product>Shoe</Product>
</Transaction>
這裏是我的XSLT。 「InvoicePeriod」應該是一個通用的格式(它的工作原理),變身爲「invoiceP」,但如何適合在這個時候,你如果犯規存在InvoicePeriod元素編寫代碼來更改默認:
<xsl:template match="Transaction" >
<Transaction invoiceP="{InvoicePeriod}" >
<xsl:sequence select="concat($InvoicePeriod, '1'[not($InvoicePeriod)])"/>
</xsl:template>
結果:
<Transaction>
<InvoiceP>1</InvoiceP>
<Product>Shoe</Product>
</Transaction>
我不清楚你的問題是什麼。你是什麼意思?「當你編寫代碼來改變默認值時,如何適應這個問題?」?我會選擇'xsl:if'或者甚至是'xsl:choose'來代替你寫的東西,但是這兩種方法都有效。你需要什麼幫助? – Borodin 2013-05-06 12:54:32
對不起,沒有說清楚。是的,我試過xsl:選擇和xsl:時。看看這裏:\t的 \t \t \t \t \t的 \t \t \t的 \t \t \t的 1 XSL:否則> \t \t \t \t \t XSL:屬性> 當xml元素不存在時,它不工作,更改爲默認'1' –
user1358072
2013-05-06 12:59:40
您的xsl:當似乎是好的。在您的示例中,僅InvoicePeriod是節點名稱而不是屬性。 Thervo在InvoicePeriod之前刪除所有'@',它應該這樣做。 '的 \t \t \t \t \t \t的 \t \t \t \t \t' –
2013-05-06 13:24:57