johnaco
johnaco

Reputation: 185

Unwanted element data (without elements) appears in xslt

I am trying to apply a transformation to generate an rdf document. My source xml contains an element that I want to ignore, but the output (generated via SAP's transformation test tool) includes the (concatenated!) texts of the elements from SYSINFO.
I have tried several variations (such as xsl:apply-templates select="asx:abap/asx:/values/CT" ) but then I get no output at all. Hmm. Have spent several hours trying different things and researching, but have gotten nowhere. Any help really appreciated. John

Here is my sample xml (fragment)

<?xml version="1.0" encoding="utf-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml"version="1.0">
<asx:values>
<SYSINFO>
    <SERVER_NAME>sapserver06</SERVER_NAME>
    <SYSTEM_ID>ECC</SYSTEM_ID>
    <SAP_RELEASE>701</SAP_RELEASE>
    <SYS_NR>00</SYS_NR>
    <CLIENT>800</CLIENT>
    <EXE_USER>XXX</EXE_USER>
    <LOGON_LANGUAGE>E</LOGON_LANGUAGE>
    <DATE>2013-04-09</DATE>
    <TIME>12:06:58</TIME>
    <TIMEZONE>CST</TIMEZONE>
    <OPERATING_SYSTEM>Windows NT</OPERATING_SYSTEM>
    <LICENSE_NUMBER>YYY</LICENSE_NUMBER>
    <SAP_CUSTOMER>ZZZ</SAP_CUSTOMER>
    <CLIENT_CATEGORY>C</CLIENT_CATEGORY>
    <LANGUAGES_INSTALLED>JED</LANGUAGES_INSTALLED>
</SYSINFO>
<CT>
<item>
    <ID>1</ID>
    <TABNAME>T000</TABNAME>
    <FIELDNAME>ADRNR</FIELDNAME>
    <KEYFLAG/>
    <ROLLNAME>CHAR10</ROLLNAME>
</item>
.....
</CT>
</asx:values>
</asx:abap>

Here is the transformation

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sap="http://www.sap.com/sapxsl" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sap_coda="http://www.sapmantics.com/sap_coda#" version="1.0">

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <rdf:RDF>
      <xsl:apply-templates/>
    </rdf:RDF>
  </xsl:template>

  <xsl:template>
    <xsl:for-each select="//item">
      <rdf:Description>
        <xsl:attribute name="rdf:about">
          <xsl:value-of select="TABNAME"/>
        </xsl:attribute>
        <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#ctable"/>
        <sap_coda:t2f>
          <rdf:Description>
            <xsl:attribute name="rdf:about">
              <xsl:value-of select="FIELDNAME"/>
            </xsl:attribute>
            <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#cfield"/>
            <xsl:if test="KEYFLAG=X">
              <sap_coda:keyflag>X</sap_coda:keyflag>
            </xsl:if>
          </rdf:Description>
        </sap_coda:t2f>
      </rdf:Description>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>

And finally, here is the output:

<?xml version="1.0" encoding="utf-16"?>
sapserver06ECC70100800XXXE2013-04-0917:21:50CSTWindowsNTYYYZZZCJED
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sap_coda="http://www.sapmantics.com/sap_coda#">
<rdf:Description rdf:about="T000">
<rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#ctable"/>....

Upvotes: 0

Views: 1144

Answers (1)

user1760725
user1760725

Reputation:

The concatenation of your <SYSINFO/> text nodes can be explained by the default handing for element nodes:

http://www.w3.org/TR/xslt#built-in-rule

I don't know which XSLT processor you are using, but I am surprised that your //item handler wasn't flagged for lacking a @match attribute. You can update it with the following, and it will handle each <item/> element in document order:

<xsl:template match="item">
    <rdf:Description>

To keep your SYSINFO content from being included in your RDF (if that is your intent), update your root template with the following:

<rdf:RDF>
    <xsl:apply-templates select="//item"/>
</rdf:RDF>

-----------EDIT--------------------------

With this input

<?xml version="1.0" encoding="utf-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml">
    <asx:values>
        <SYSINFO>
            <SERVER_NAME>sapserver06</SERVER_NAME>
            <SYSTEM_ID>ECC</SYSTEM_ID>
            <SAP_RELEASE>701</SAP_RELEASE>
            <SYS_NR>00</SYS_NR>
            <CLIENT>800</CLIENT>
            <EXE_USER>XXX</EXE_USER>
            <LOGON_LANGUAGE>E</LOGON_LANGUAGE>
            <DATE>2013-04-09</DATE>
            <TIME>12:06:58</TIME>
            <TIMEZONE>CST</TIMEZONE>
            <OPERATING_SYSTEM>Windows NT</OPERATING_SYSTEM>
            <LICENSE_NUMBER>YYY</LICENSE_NUMBER>
            <SAP_CUSTOMER>ZZZ</SAP_CUSTOMER>
            <CLIENT_CATEGORY>C</CLIENT_CATEGORY>
            <LANGUAGES_INSTALLED>JED</LANGUAGES_INSTALLED>
        </SYSINFO>
        <CT>
            <item>
                <ID>1</ID>
                <TABNAME>T000</TABNAME>
                <FIELDNAME>ADRNR</FIELDNAME>
                <KEYFLAG/>
                <ROLLNAME>CHAR10</ROLLNAME>
            </item>
        </CT>
    </asx:values>
</asx:abap>

and this stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:sap="http://www.sap.com/sapxsl"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:sap_coda="http://www.sapmantics.com/sap_coda#" 
    version="1.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <rdf:RDF>
            <xsl:apply-templates select="//item"/>
        </rdf:RDF>
    </xsl:template>

    <xsl:template match="item[parent::CT]">
        <rdf:Description>
            <xsl:attribute name="rdf:about">
                <xsl:value-of select="TABNAME"/>
            </xsl:attribute>
            <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#ctable"/>
            <sap_coda:t2f>
                <rdf:Description>
                    <xsl:attribute name="rdf:about">
                        <xsl:value-of select="FIELDNAME"/>
                    </xsl:attribute>
                    <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#cfield"/>
                    <xsl:if test="KEYFLAG=X">
                        <sap_coda:keyflag>X</sap_coda:keyflag>
                    </xsl:if>
                </rdf:Description>
            </sap_coda:t2f>
        </rdf:Description>
    </xsl:template>

    <xsl:template match="item[parent::FOO]"/> 

    <xsl:template match="item"/> 
</xsl:transform>

I get this output from Saxon-EE 9.4.0.3:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:sap="http://www.sap.com/sapxsl"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:sap_coda="http://www.sapmantics.com/sap_coda#">
   <rdf:Description rdf:about="T000">
      <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#ctable"/>
      <sap_coda:t2f>
         <rdf:Description rdf:about="ADRNR">
            <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#cfield"/>
         </rdf:Description>
      </sap_coda:t2f>
   </rdf:Description>
</rdf:RDF>

and this output from xstlproc:

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sap="http://www.sap.com/sapxsl" xmlns:sap_coda="http://www.sapmantics.com/sap_coda#">
  <rdf:Description rdf:about="T000">
    <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#ctable"/>
    <sap_coda:t2f>
      <rdf:Description rdf:about="ADRNR">
        <rdf:type rdf:resource="http://www.sapmantics.com/sap_coda#cfield"/>
      </rdf:Description>
    </sap_coda:t2f>
  </rdf:Description>
</rdf:RDF>

Upvotes: 1

Related Questions