Reputation: 948
i need to generate this xml with all things, namespaces on each node etc
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos"
xmlns:bdo_fosfec="http://asocajas.app.com/example"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512>39756656</C512>
<C614>YAXMINNI</C614>
</registro82>
</registro54>
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512>79374740</C512>
<C614>VICTOR</C614>
</registro82>
</registro54>
</bdo_fosfec:RegistrosPagosElement>
And i build a this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos" xmlns:bdo_fosfec="http://asocajas.app.com/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates select="registro54"/>
</bdo_fosfec:RegistrosPagosElement>
</xsl:template>
<!--TEMPLATE REGISTRO 54-->
<xsl:template match="registro54">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512><xsl:value-of select="C512"/></C512>
<C614><xsl:value-of select="C614"/></C614>
</registro82>
</registro54>
</xsl:template>
</xsl:stylesheet>
but when i transform myxml with the XSLT the result xml is not as expected
result:
<?xml version="1.0" encoding="utf-8"?>
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos" xmlns:bdo_fosfec="http://asocajas.app.com/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
myxml
<?xml version="1.0" encoding="utf-8"?>
<bdo_fosfec_x003A_RegistrosPagosElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<registro54>
<registro82>
<C512>123456789</C512>
<C614>Miguel</C614>
</registro82>
</registro54>
<registro54>
<registro82>
<C512>1234567890</C512>
<C614>Jerónimo</C614>
</registro82>
</registro54>
</bdo_fosfec_x003A_RegistrosPagosElement>
I do not know what I'm doing wrong, I've tried removing the nameSpace xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: type = "bdo_fosfec: RegistersPages" xmlns: bdo_fosfec = "http: //asocajas.hp.com/bdo_fosfec "of the stylesheet but it generates error, I also tried not to use" templates "and the result approaches the desired one but it is not the same that I hope
Thnks
Upvotes: 1
Views: 384
Reputation: 1180
It looks like you're getting a bit confused about context, and this is leading to faulty XPath selectors.
You start here:
<xsl:template match="/">
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos" xmlns:bdo_fosfec="http://asocajas.app.com/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates select="registro54"/>
</bdo_fosfec:RegistrosPagosElement>
</xsl:template>
So you match
ed on the logical root element. Within the context of this logical root element, you use xsl:apply-templates
-- but with select="registro54"
. This XPath is looking for any registro54
elements that are immediate children of the context element. However, the immediate child of the logical root is the topmost element in the file, in your case, bdo_fosfec_x003A_RegistrosPagosElement
. So this select
statement selects nothing.
We can fix this by changing one of two things. Either:
xsl:template match
statement to xsl:template match="/bdo_fosfec_x003A_RegistrosPagosElement"
,xsl:apply-templates select
statement to xsl:apply-templates select="bdo_fosfec_x003A_RegistrosPagosElement/registro54"
.Even after implementing this first fix, we don't get what we need. Your second template:
<xsl:template match="registro54">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512><xsl:value-of select="C512"/></C512>
<C614><xsl:value-of select="C614"/></C614>
</registro82>
</registro54>
</xsl:template>
So our context is the registro54
element. We try to get values using these two statements:
<C512><xsl:value-of select="C512"/></C512>
<C614><xsl:value-of select="C614"/></C614>
These produce nothing, again because the XPaths select nothing. Within the context of registro54
, these select
statements try to find immediate children of registro54
that are named C512
or C614
.
However, if we look at your input XML:
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512>39756656</C512>
<C614>YAXMINNI</C614>
</registro82>
</registro54>
... we see that the C512
and C614
elements are children of registro82
instead. So to actually get these values, change your select
statements to:
<C512><xsl:value-of select="registro82/C512"/></C512>
<C614><xsl:value-of select="registro82/C614"/></C614>
Remember that select
with relative XPaths (any XPath expression that doesn't start with /
, the logical root) will only ever select from the starting point of the context element.
Upvotes: 2