0

我做了 XSLT 转换。

我缺少的是 nil 属性。我的意思是,如果源元素具有 nil 元素 true,我想将其映射到目标 XML。

<xsl:if 
test="string-length(soapenv:Envelope/soapenv:Body/b:getBLResponse/b:result/BResult:BLOut/Class:ID)=0">
    <xsl:attribute name="i:nil">true</xsl:attribute>
</xsl:if>

if 以上适用于特定节点,但我想将其作为通用模板,而不是检查每个字段

可能可以创建将接收 xml 节点的模板,如果节点具有 nil 属性,则将进行验证,否则它将返回 nil 属性,否则没有 nil 属性。

下面是例子

零:输入:

<TEST>
    <Child i:nil="true">asdf</Child>
</TEST>
Output:

<TEST xmlns:i="whatever" >
    <OutputChild i:nil="true">asdf</OutputChild >
</TEST>

Without nil: Input + Output the same

<TEST>
    <OutputChild >example</OutputChild >
</TEST>
4

2 回答 2

0

我不确定这是否正是您要查找的内容(请记住始终包含输入和所需的输出 XML),但是在这里您有一个通用模板,它在应用任何其他处理之前递归地查找空节点和属性(如果您不需要属性检查,只需删除“或”之后的部分):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="whatever">
    <xsl:output method="xml" indent="yes"/>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <xsl:template match="/*">
        <xsl:copy>
            <xsl:variable name="nil">
                <xsl:apply-templates select="." mode="nil"/>
            </xsl:variable>
            <xsl:if test="$nil='true'">
                <xsl:attribute name="i:nil">true</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <xsl:template match="*" mode="nil">
        <xsl:choose>
            <xsl:when test="string-length(.)=0 or @*[string-length(.)=0]">
                <xsl:value-of select="'true'"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="*" mode="nil"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

零:输入:

<TEST>
    <Child test="aaa" secondtest="">asdf</Child>
</TEST>

输出:

<TEST xmlns:i="whatever" i:nil="true">
    <Child test="aaa" secondtest="">asdf</Child>
</TEST>

没有零:输入+输出(什么都不做):

<TEST>
    <Child test="aaa" secondtest="bbb">asdf</Child>
</TEST>
于 2012-11-07T13:56:23.087 回答
0

为了解决它,我编写了模板,它接收映射节点和新元素名称。检查元素是否为空后,模板返回 nil 属性

<xsl:template name="TransformNode" >
        <xsl:param name="pCurrentNode"/>
        <xsl:param name="elementName"/>
        <xsl:element name = "{$elementName}">
            <xsl:if test="$pCurrentNode/@i:nil='true'"><xsl:attribute name="nil">true</xsl:attribute></xsl:if>      
            <xsl:value-of select="$pCurrentNode"/>
        </xsl:element>
    </xsl:template>
于 2012-11-07T15:56:11.593 回答