xslt条件转换

xslt Conditional Transformation

我正在尝试想出一个 xslt,它要么将所有内容从源 xml 复制到目标,要么根据源文件中的特定值生成一个空文件。

假设我有 source1.xml,如下所示:

<Order>
  <isDigitalProduct>true</isDigitalProduct>
  <productID>1234</productID>
<Order>

和source2.xml,像下面这样:

<Order>
  <isDigitalProduct>false</isDigitalProduct>
  <productID>5678</productID>
<Order>

如何修改我的 xslt 来评估 <isDigitalProduct> 的值,以便当它的值为 "true" 时,按原样复制所有内容,并在其值为 [= 时生成空白输出31=]?对于上面的示例,source1.xml 将复制其内容,而转换后的 source2.xml 将生成一个空白文件。

感谢任何帮助!

还有一个问题,如果不是复制我需要的所有内容,而是将 <isDigitalProduct> 元素转换为 <SerialNumber>,会怎样呢?例如,source2.xml 仍然转换为空输出,而 source1.xml 转换为:

<Order>
  <SerialNumber>ABC</SerialNumber>
  <productID>1234</productID>
<Order>

谢谢!

类似的东西应该可以工作,但是如果生成的树是空的,你很可能会遇到一些错误...

<xsl:template match="Order">
   <xsl:choose>
     <xsl:when test="isDigitalProduct/text() = 'true'">
       <xsl:copy-of select="."/>
     </xsl:when>
     <xsl:otherwise>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

How can I modify my xslt to evaluate the value of the <isDigitalProduct> so that when its value is "true", copy everything as is, and produce a blank output when its value is "false"?

如果这是全部你想做的,你可以简单地做:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>


<xsl:template match="/">
    <xsl:copy-of select="Order[isDigitalProduct='true']"/>
</xsl:template>

</xsl:stylesheet>

One more question, what if instead of copy everything I need to transform the <isDigitalProduct> element into <SerialNumber>.

在这种情况下,您无需按原样复制 Order ,而是对其应用模板 - 并在该模板中进行任何必要的修改,例如示例:

<xsl:template match="/">
    <xsl:apply-templates select="Order[isDigitalProduct='true']"/>
</xsl:template>

<xsl:template match="Order">
    <xsl:copy>
        <SerialNumber>ABC</SerialNumber>
        <xsl:copy-of select="productID"/>
    </xsl:copy>
</xsl:template>