문제

I did the XSLT transformation.

What I missing is nil attribute. I mean that if source element has nil element true I want to map it to destination 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>

The if above working for specific node , but I want to make it as general template, not to check every field

May be it is possible to create template which will receive xml node, than will be validation if node has nil attribute if it is , it will return with the nil attribute otherwise without nil attribute.

Below is example

With nil: Input:

<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>
도움이 되었습니까?

해결책 2

In order to solve it , I wrote the template, which receives mapping node, and new element name. After checking if element is null, the template return nil attribute

<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>

다른 팁

I'm not sure if this is exactly what you are looking for (PLEASE: remember to always include input and desired output XML), but here you have a generic template which looks recuresively for empty nodes and attributes before applying any additional processing (if you don't need the attributes check just remove the part after the "or"):

<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>

With nil: Input:

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

Output:

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

Without nil: Input + Output (does nothing):

<TEST>
    <Child test="aaa" secondtest="bbb">asdf</Child>
</TEST>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top