0

I know that this is simple problem. I'm still learning and getting familiarize with the XSLT coding. I have a problem in my XSLT and I don't know if I did it correctly. I need to get the value from the input file and store it in the new element tag name and that I don't need to populate the namespaces and attributes what's on the parent root element. I did a research about this and I saw many references but I can't apply it. The XSLT(v02) that I made is working fine (just copy from the references) if the root element doesn't have any namespaces and attributes. But, when I put a namespaces and attribute, no output populated.

Input file

<Root xmlns="http://abcd.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="9.2" versionID="2.12.3" xsi:schemaLocation="abcd.com abcd.xsd">
    <Element>
        <Field>AAAAA</Field>
    </Element>
    <Element>
        <Field>BBBBB</Field>
    </Element>
    <Element>
        <Field>CCCCC</Field>
    </Element>

xslt file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <NewRecord>
        <xsl:for-each select="Root/Element">
            <NewTransaction>
                <Position>
                    <xsl:value-of select="position()"/>
                </Position>
                <TransactionID>
                    <xsl:value-of select="Field"/>
                </TransactionID>
            </NewTransaction>
        </xsl:for-each>
    </NewRecord>
</xsl:template>

output generated

<NewRecord/>

My expected output should look like this:

<NewRecord>
<NewTransaction>
    <Position>1</Position>
    <TransactionID>AAAAA</TransactionID>
</NewTransaction>
<NewTransaction>
    <Position>2</Position>
    <TransactionID>BBBBB</TransactionID>
</NewTransaction>
<NewTransaction>
    <Position>3</Position>
    <TransactionID>CCCCC</TransactionID>
</NewTransaction>

I think the problem is in the <xsl:template match="/">, I'm still confused on the nodes that I need to put. Thank you for your help.

Yoona May
  • 93
  • 8
  • Possible duplicate of [XSLT with XML source that has a default namespace set to xmlns](http://stackoverflow.com/questions/1344158/xslt-with-xml-source-that-has-a-default-namespace-set-to-xmlns) – teppic Dec 20 '16 at 05:36
  • Why does your stylesheet declare `version="1.0"` when your question is tagged `xslt-2.0`? – michael.hor257k Dec 20 '16 at 15:09
  • @michael. hor257k, i forgot to change the version but I really using v02. Thank you. – Yoona May Dec 20 '16 at 23:47
  • @teppic, thank you for the reference. I will add this for my references :) – Yoona May Dec 20 '16 at 23:47

2 Answers2

1

If you're using xslt 1.0, you'll have to declare the same namespace in the stylesheet, and use the prefix you map to the namespace to qualify the names of the elements:

The prefix can be whatever you want. I picked abcd to match your example, but it could be any legal identifier.

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:abcd="http://abcd.com">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <NewRecord>
            <xsl:for-each select="abcd:Root/abcd:Element">
                <NewTransaction>
                    <Position>
                        <xsl:value-of select="position()"/>
                    </Position>
                    <TransactionID>
                        <xsl:value-of select="abcd:Field"/>
                    </TransactionID>
                </NewTransaction>
            </xsl:for-each>
        </NewRecord>
    </xsl:template>
</xsl:stylesheet>
teppic
  • 7,051
  • 1
  • 29
  • 35
1

If you are really using XSLT 2.0, you only need to add:

xpath-default-namespace="http://abcd.com"

to the stylesheet tag, and leave everything else as is.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51