Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5849223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:58:11+00:00 2026-05-22T12:58:11+00:00

I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8"?> <ExportData> <TransportHeader> <Timestamp>2011-01-16 06:00:33</Timestamp>

  • 0

I have the following simplified XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<TransportHeader>
    <Timestamp>2011-01-16 06:00:33</Timestamp>
    <From>
        <Name>DynamicExport</Name>
        <Version>1.</Version>
    </From>
    <MessageId>d7b5c5b69a83</MessageId>
</TransportHeader>
<ExportConfig>
    <DateTimeFormat>yyyy-MM-dd HH:mm:ss</DateTimeFormat>
    <DecimalSymbol>.</DecimalSymbol>
</ExportConfig>
<DataSet> 
    <Tables>
        <Table>
            <RH>...</RH>
            <Rows>
                <R>Data1</R>
                <R>Data2</R>
                <R>Data3</R>
                <R>Data4</R>
                <R>Data5</R>
            </Rows>
        </Table>
    </Tables>
</DataSet>
</ExportData>

I have to check if <R> elements exist or not. If no <R> elements exist the mapping has to be aborted, otherwise a <Line> element per <R> needs to be created.

I came up with this solution which works perfectly so far:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="ISO-8859-1" method="xml" indent="yes" />

<!-- suppress nodes that are not matched -->
<xsl:template match="text() | @*">
    <xsl:apply-templates select="text() | @*"/>
</xsl:template>

<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="not(ExportData/DataSet/Tables/Table/Rows/node())">
            <xsl:message terminate="yes">No line items</xsl:message>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="/ExportData/DataSet/Tables/Table/Rows">
    <INVOIC02>
        <!-- apply LINE ITEMS template -->
        <xsl:apply-templates select="R"/>
    </INVOIC02>
</xsl:template>

<!-- Template creating LINE ITEMS -->
<xsl:template match="R">
    
    <Line>
        <elements></elements>
    </Line>
</xsl:template>


</xsl:stylesheet>

If there are <R> elements the output is this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<INVOIC02>
<Line>
    <elements/>
</Line>
<Line>
    <elements/>
</Line>
<Line>
    <elements/>
</Line>
<Line>
    <elements/>
</Line>
<Line>
    <elements/>
</Line>
</INVOIC02>

If there is just <Rows/> and no <R>s the mapping is aborted.

Now I have two questions:

-Is my test for <R> elements robust: test="not(ExportData/DataSet/Tables/Table/Rows/node())" ?

-I am using <xsl:apply-templates> to create the <Line> items instead of an <xsl:for-each> construct. Are my XPath expressions okay or could I make them better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:58:11+00:00Added an answer on May 22, 2026 at 12:58 pm

    Is my test for elements robust: test=”not(ExportData/DataSet/Tables/Table/Rows/node())”
    ?

    Well, do you want it to fail if there are no R elements, or fail if Rows does not have a child node(), which would include any element (not just R), text(), comment() or processing-instruction()?

    If you really want to verify that there is at least one R element that is a child of Rows, you should adjust the test criteria to be more specific:

    test="not(ExportData/DataSet/Tables/Table/Rows/R)"
    

    Otherwise, it may pass that test and continue processing and not generate the content you want.

    I am using <xsl:apply-templates> to create the <Line> items instead of an 
    <xsl:for-each> construct. 
    Are my XPath expressions okay or could I make them better?
    

    You could get rid of the <xsl:if> conditional logic inside of your template for the root node and move that logic into a template for Rows that don’t contain R children. Putting logic into xsl:template @match criteria makes it easier for XSLT processors to optimize, which can lead to performance gains.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output encoding="ISO-8859-1" method="xml" indent="yes" />
    
        <!-- suppress nodes that are not matched -->
        <xsl:template match="text() | @*">
            <xsl:apply-templates select="text() | @*"/>
        </xsl:template>
    
        <!--All Rows must contain an R.  
            If we encounter any that do not, terminate the transform -->
        <xsl:template match="/ExportData/DataSet/Tables/Table/Rows[not(R)]">
            <xsl:message terminate="yes">No line items</xsl:message>
        </xsl:template>
    
        <!--match for Rows that have R children -->
        <xsl:template match="/ExportData/DataSet/Tables/Table/Rows[R]">
            <INVOIC02>
                <!-- apply LINE ITEMS template -->
                <xsl:apply-templates select="R"/>
            </INVOIC02>
        </xsl:template>
    
        <!-- Template creating LINE ITEMS -->
        <xsl:template match="R">      
            <Line>
                <elements></elements>
            </Line>
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have an xml file with the following simplified xml file contents: <CollectionItems>
I have the following (highly simplified) XML document that I am reading into my
I have the following XML (simplified): <node1> <node2> <node3> </node3> </node2> </node1> And I
I have the following XML (it is simplified and most attributes are omitted): <Document>
Consider the following simplified version of my code. I have a template class A
I have the following model (greatly simplified for the purposes of this question): class
The Facts I have the following datastructure consisting of a table and a list
I have an XML File that I am processing using LINQ. I want to
Folks, i have the following problem. My website uses an iframe to select an
Apologies in advance for the length of this question! I have a data structure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.