1

I'm struggling to test to see if an element exists. If it doesn't, I'd like to add in a default value. Here's my XML:

<Transaction>
 <InvoicePeriod>1</InvoicePeriod>
 <Product>Shoe</Product>
</Transaction>

If InvoicePeriod element doesnt exist, default should be 1 :

<Transaction>
 <Product>Shoe</Product>
</Transaction>

Here is my XSLT. "InvoicePeriod" is supposed to transform into "invoiceP" in a generic format (it works), but how to fit in this when you write code to change default if InvoicePeriod element doesnt exist:

<xsl:template match="Transaction" >
  <Transaction invoiceP="{InvoicePeriod}" >
  <xsl:sequence select="concat($InvoicePeriod, '1'[not($InvoicePeriod)])"/>
</xsl:template>

Result:

<Transaction>
 <InvoiceP>1</InvoiceP>
 <Product>Shoe</Product>
</Transaction>
4

3 回答 3

0

尝试这样的事情:

 <?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
    <xsl:output method="xml"  indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="def_InvoicePeriod" select="'1'" />

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

    <xsl:template match="Transaction[not(InvoicePeriod)]" >
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <invoiceP>
                <xsl:value-of select="$def_InvoicePeriod"/>
            </invoiceP>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

def_InvoicePeriod 可以在调用转换时更改。例如使用 xslptorc:

xsltproc --stringparam def_InvoicePeriod 2  xsltfile xmlfile

更新:(似乎 invoiceP 应该是 Transaction 中的一个属性,默认值也应该用于空值或 value="0"。

尝试这个:

<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
    <xsl:output method="xml"  indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:param  name="def_InvoicePeriod" select="'1'" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="InvoicePeriod" />
    <xsl:template match="Transaction" >
        <xsl:copy>
            <xsl:attribute name="invoiceP">
                <xsl:choose>
                    <xsl:when test="number(InvoicePeriod) &gt; '0' ">
                        <xsl:value-of select="InvoicePeriod"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$def_InvoicePeriod"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

更新测试输入:

<?xml version="1.0"?>
<xml>
    <Transaction>
        <InvoicePeriod>1</InvoicePeriod>
        <Product>Shoe</Product>
    </Transaction>
    <Transaction>
        <InvoicePeriod>3</InvoicePeriod>
        <Product>Shoe1</Product>
    </Transaction>
    <Transaction>
        <InvoicePeriod>0</InvoicePeriod>
        <Product>Shoe2</Product>
    </Transaction>
    <Transaction>
        <Product>Shoe3</Product>
    </Transaction>
</xml>

输出:

<?xml version="1.0"?>
<xnl>
  <Transaction invoiceP="1">
    <Product>Shoe</Product>
  </Transaction>
  <Transaction invoiceP="3">
    <Product>Shoe1</Product>
  </Transaction>
  <Transaction invoiceP="1">
    <Product>Shoe2</Product>
  </Transaction>
  <Transaction invoiceP="1">
    <Product>Shoe3</Product>
  </Transaction>
</xnl>
于 2013-05-06T13:01:42.927 回答
0

看起来你看起来像:

  <xsl:template match="Transaction">
    <Transaction>
      <xsl:if test="not(InvoicePeriod)">
        <InvoiceP>1</InvoiceP>
      </xsl:if>
      <xsl:apply-templates/>
    </Transaction>
  </xsl:template>
于 2013-05-06T13:03:12.437 回答
0

您的代码(您在评论中显示)的问题是您正在查看@InvoicePeriod哪个属性Transaction永远不会出现。您需要简单地检查InvoicePeriod哪个是该名称的子元素。

如果您确实需要发票期间作为属性InvoiceP,那么您需要这样的东西。我还复制了Product元素,因为我不知道还能用它做什么。

<xsl:template match="Transaction">
    <xsl:copy>
        <xsl:attribute name="InvoiceP">
            <xsl:choose>
                <xsl:when test="InvoicePeriod and InvoicePeriod > 0">
                    <xsl:value-of select="InvoicePeriod"/>
                </xsl:when>
                <xsl:otherwise>1</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:copy-of select="Product"/>
    </xsl:copy>
</xsl:template>

输出

 <Transaction InvoiceP="1">
     <Product>Shoe</Product>
 </Transaction>
于 2013-05-06T14:03:16.197 回答