0

I have an XSLT file that I want to use to create two separate XML files/Strings. The problem is I can't use the same template matching.

If I have this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="Frame/AAA">
        <xsl:for-each select=".">
            <Frame xmlns="MyNamespace.com">
                <BBB>
                    <!-- Stuff here -->
                </BBB>
            </Frame>
        </xsl:for-each>
     </xsl:template>

    <xsl:template match="Frame/AAA">
        <xsl:for-each select=".">
            <Frame xmlns="MyNamespace.com">
                <WWW>
                    <!-- Stuff here -->
                </WWW>
            </Frame>
        </xsl:for-each>
     </xsl:template>

</xsl:stylesheet>

XML file:

<Frame>
    <AAA>
        <!-- Stuff here -->
    </AAA>
<Frame>

So I want to use both templates and create two XML files. However, using two of the same templates is not allowed as it won't know where to look.

This is the Java code I use to create the XML files:

// Get stylesheet (xslt) and xml data file
File stylesheet = new File(xsltFilepath);
InputSource inputSource = new InputSource(new ByteArrayInputStream(xmlString.getBytes()));

// Turn data file into document
Document document = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder().parse(inputSource);

// Hold XML markup
StreamSource stylesource = new StreamSource(stylesheet);
// Turn source into a transformer object
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
// Convert to a string
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));

// Return the string
return tringWriter.toString();

How can I achieve what I want?

2
  • You can only create two separate result documents if you Saxon 9 and XSLT 2.0 and xsl:result-document or Xalan specific extensions. As for processing a node twice with different templates, use modes to distinguish the templates. Commented Jun 28, 2016 at 20:40
  • Thanks, I'll look into XSLT 2.0 and using modes. If you can provide an example as well, it would be very helpful. Commented Jun 28, 2016 at 20:47

1 Answer 1

1

If you use XSLT 2.0 and modes you can use e.g.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="/">
       <xsl:apply-templates/>
       <xsl:apply-templates mode="m2"/>
    </xsl:template>

    <xsl:template match="Frame/AAA">

            <Frame xmlns="MyNamespace.com">
                <BBB>
                    <!-- Stuff here -->
                </BBB>
            </Frame>

     </xsl:template>

    <xsl:template match="Frame/AAA" mode="m2">
        <xsl:result-document href="result2.xml">
            <Frame xmlns="MyNamespace.com">
                <WWW>
                    <!-- Stuff here -->
                </WWW>
            </Frame>
        </xsl:result-document>
     </xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! However, where does it save it? I thought it would save the file result2.xml in the same folder as the xslt but there's nothing there. I tried giving it a path: "C:/users/mo/Documents/Project/result2.xml" but it says the URI is malformed (Doesn't know what 'c' is).
A local file system URI that is absolute is file:///C:/users/mo/Documents/Project/result2.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.