users@jaxb.java.net

Re: Absolute XPath in a variable containing a set of nodes obtained with document() function (SOLVED)

From: Ely Schoenfeld <ely.sun.1_at_mitalteli.com>
Date: Fri, 28 May 2010 11:10:13 -0500

Hi Wolfgang.

Again: Thank you for your help.

I have to tell I was going crazy. Absolutely crazy.

Then... when I decided to do it in a two step manner in which I first
combined both documents (xml) and then processed the resulting single
file to convert it to an html report, I discovered that it also gave me
empty values.

Because I'm so new to this, I didn't notice the importance of namespaces.

The examples I sent here, where "edited". I shouldn't have.

The whole problem was that the main xml documents had defined
'xmlns="..."' and the other one didn't.

All I needed to do was adding 'xmlns="" xmlns:m="..."' to the
<xsl:stylesheet headder and each time I need to read data from the main
document I need to add the 'm:' prefix.

That was it.

Forgive me for making you loose your time.

Ely.

Wolfgang Laun escribió:
> If I haven't lost you, then I think that prefixing your absolute paths with
> root($main) or root($extraData) should let you access whatsoever from the
> two documents.
>
> -W
>
>
> On 27 May 2010 02:50, Ely Schoenfeld <ely.sun.1_at_mitalteli.com> wrote:
>
>> It's absolutely incredible.
>>
>> I decided to simplify my test cases and I discovered that almost the same
>> instruction works perfectly with the ExtraData.xml, but returns empty values
>> with the main one.
>>
>> As you can see later on, both lines with "aaa" read the main file, and both
>> lines with "bbb" read the "extraData" file. the first two lines use the
>> variable containing the "document()" functions output, and the second two
>> lines execute the "document()" function directly.
>>
>> The line that begins with "first rfc value" demonstrates that the "main"
>> xml is found and loaded, because it shows a value.
>>
>> Any ideas about what could I be doing wrong or what could be the problem?
>>
>> This is the xslt:
>>
>>
>> XSLT:
>> ------------------------ BEGIN -------------------------
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>>
>> <xsl:output method = "html" />
>>
>>
>> <xsl:template match="/">
>> <xsl:variable name="main" select="document(/files/file,
>> /)"/>
>> <xsl:variable name="extraData"
>> select="document(/files/fileExtraData,
>> /)"/>
>>
>> aaa1 [<xsl:value-of
>> select="$main/Comprobante/Receptor/_at_rfc"/>]
>> aaa<br></br>
>> bbb1 [<xsl:value-of
>> select="$extraData/ComprobanteExtraData/RECEPTOR/atributo[@name='CODCLI']/_at_value"/>]
>>
>> bbb<br></br>
>> aaa2 [<xsl:value-of select="document(/files/file,
>> /)/Comprobante/Receptor/_at_rfc"/>] aaa<br></br>
>> bbb2 [<xsl:value-of select="document(/files/fileExtraData,
>> /)/ComprobanteExtraData/RECEPTOR/atributo[@name='CODCLI']/_at_value"/>] bbb
>> <br></br>
>> first rfc value: [<xsl:value-of select="$cfd//_at_rfc"/>] <br></br>
>> <br></br>
>> </xsl:template>
>> ------------------------ END ---------------------------
>>
>>
>> MAIN XML:
>>
>> ------------------------ BEGIN -------------------------
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <Comprobante>
>> <Emisor rfc="SDF531211GE2" nombre="NOMBRE">
>> <DomicilioFiscal calle="DOMFIS_CALLE"
>> noExterior="DOMFIS_NOEXTERIOR" noInterior="DOMFIS_NOINTERIOR"
>> colonia="DOMFIS_COLONIA" localidad="DOMFIS_LOCALIDAD"
>> referencia="DOMFIS_REFERENCIA" municipio="DOMFIS_MUNICIPIO"
>> estado="DOMFIS_ESTADO" pais="DOMFIS_PAIS" codigoPostal="43221"/>
>> <ExpedidoEn calle="EXP_CALLE" noExterior="EXP_NOEXTERIOR"
>> noInterior="EXP_NOINTERIOR" colonia="EXP_COLONIA"
>> localidad="EXP_LOCALIDAD" referencia="EXP_REFERENCIA"
>> municipio="EXP_MUNICIPIO" estado="EXP_ESTADO" pais="EXP_PAIS"
>> codigoPostal="EXP_CODIGOPOSTAL"/>
>>
>> </Emisor>
>> <Receptor rfc="SEFT760217GA1" nombre="NOMBRE">
>> <Domicilio calle="DOM_CALLE" noExterior="DOM_NOEXTERIOR"
>> noInterior="DOM_NOINTERIOR" colonia="DOM_COLONIA"
>> localidad="DOM_LOCALIDAD" referencia="DOM_REFERENCIA"
>> municipio="DOM_MUNICIPIO" estado="DOM_ESTADO" pais="DOM_PAIS"
>> codigoPostal="86421"/>
>> </Receptor>
>> ...
>> ------------------------ END ---------------------------
>>
>>
>> EXRA DATA XML:
>>
>> ------------------------ BEGIN -------------------------
>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>> <ComprobanteExtraData>
>> <GENERAL>
>> <atributo name="FECHAPAG" value="18/02/2010"/>
>> <atributo name="TRANSPORTISTA" value="Pancho el transportador"/>
>> <atributo name="OBS" value="Unas observaciones generales"/>
>> </GENERAL>
>> <EMISOR>
>> <atributo name="FECHAPAG" value="18/02/2010"/>
>> <atributo name="TRANSPORTISTA" value="Pancho el transportador"/>
>> <atributo name="OBS" value="Unas observaciones generales"/>
>> </EMISOR>
>> <RECEPTOR>
>> <atributo name="CODCLI" value="a1234"/>
>> <atributo name="CODCLI" value="1234"/>
>> </RECEPTOR>
>> ...
>> ------------------------ END ---------------------------
>>
>>
>> The XSLT gives the following output:
>>
>> ------------------------ BEGIN -------------------------
>> aaa1 [] aaa
>> bbb1 [a1234] bbb
>> aaa2 [] aaa
>> bbb2 [a1234] bbb
>> first rfc value: [SDF531211GE2]
>> ------------------------ END ---------------------------
>>
>> As you can see, almost the same instruction works with one of the xml files
>> and it doesn't with the other xml file.
>>
>> Thanks in advance.
>>
>>
>> Ely.
>>
>>
>> Ely Schoenfeld escribió:
>>
>>> I forgot to show how I'm "jumping" into the $main variable contents:
>>>
>>> XSLT:
>>> ------------------------ BEGIN -------------------------
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xsl:stylesheet version="1.0" xmlns:xsl="
>>> http://www.w3.org/1999/XSL/Transform">
>>>
>>> <xsl:template match="/">
>>> <xsl:variable name="main" select="document(/files/file, /)"/>
>>> <xsl:variable name="extraData" select="document(/files/fileExtraData,
>>> /)"/>
>>>
>>> <xsl:apply-templates select="$main" mode="process"/> </xsl:template>
>>>
>>> <xsl:template match="/" mode="process">
>>> 0 [<xsl:value-of select="//_at_rfc"/>]<br></br>
>>> 1 [<xsl:value-of select="/Comprobante/Receptor/_at_rfc"/>]<br></br>
>>> ...
>>> </xsl:template>
>>> ------------------------ BEGIN -------------------------
>>>
>>> Any pointers on how to print the Receptor's rfc attribute would be very
>>> appreciated.
>>>
>>> Thank you all in advance.
>>>
>>> Ely.
>>>
>>>
>>> Ely Schoenfeld escribió:
>>>
>>>> Maybe this is not the right place to ask general XML-XSLT-XPATH
>>>> questions.
>>>>
>>>> Any pointers to a better place would be appreciated.
>>>>
>>>> In the mean time:
>>>>
>>>> In may way to merging two xml files using XSLT, I decided to use one XML
>>>> file containing the names of the files to be merged:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>> <?xml version="1.0" encoding="ISO-8859-1"?>
>>>> <?xml-stylesheet type="text/xsl"
>>>> href="catalog/xslt/facturaEnPapel.xslt"?>
>>>> <files>
>>>> <file>main.xml</file>
>>>> <fileExtraData>extraData.xml</fileExtraData>
>>>> </files>
>>>> ------------------------ END ---------------------------
>>>>
>>>> And the "facturaEnPapel.xslt" is loading both files contents with:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <xsl:stylesheet version="1.0" xmlns:xsl="
>>>> http://www.w3.org/1999/XSL/Transform">
>>>>
>>>> <xsl:variable name="main" select="document(/files/file, /)"/>
>>>> <xsl:variable name="extraData" select="document(/files/fileExtraData,
>>>> /)"/>
>>>>
>>>> ...
>>>> ------------------------ END ---------------------------
>>>>
>>>>
>>>> My problem is I can't seem to find how to get a particular node's value
>>>> using an "absolute" XPath.
>>>>
>>>> For example... the "main" document contains:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
>>>> <Comprobante version="2.0" serie="ARH" folio="7909">
>>>> <Emisor rfc="SDF531211GE2" nombre="NOMBRE1">
>>>> ...
>>>> </Emisor>
>>>> <Receptor rfc="SEFT760217GA1" nombre="NOMBRE2">
>>>> ...
>>>> </Receptor>
>>>> ...
>>>> ------------------------ END ---------------------------
>>>>
>>>> Imagine I want to obtain the second rfc (the one in "Receptor").
>>>> Here I show some tests, and their results:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>> 0 [<xsl:value-of select="//_at_rfc"/>]<br></br> 1 [<xsl:value-of
>>>> select="/Comprobante/Receptor/_at_rfc"/>]<br></br> 2 [<xsl:value-of
>>>> select="Comprobante/Receptor/_at_rfc"/>]<br></br> 3 [<xsl:value-of
>>>> select="//Comprobante/Receptor/_at_rfc"/>]<br></br> 4 [<xsl:value-of
>>>> select="/Receptor/_at_rfc"/>]<br></br> 5 [<xsl:value-of
>>>> select="Receptor/_at_rfc"/>]<br></br> 6 [<xsl:value-of
>>>> select="//Receptor/_at_rfc"/>]<br></br> ........ results .........
>>>>
>>>> 0 [SDF531211GE2]
>>>> 1 []
>>>> 2 []
>>>> 3 []
>>>> 4 []
>>>> 5 []
>>>> 6 []
>>>>
>>>> ------------------------ END -------------------------
>>>>
>>>> So I tried to find what parents does the "rfc" containing node.
>>>>
>>>> I used the following XSLT code:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>>
>>>> ...
>>>>
>>>> ==[<br></br><xsl:apply-templates select="//_at_rfc"/>]==<br></br>
>>>>
>>>> ...
>>>>
>>>> <xsl:template match="@rfc">
>>>> #[<xsl:call-template name="parents"/>]#<br></br>
>>>> </xsl:template>
>>>>
>>>> ...
>>>>
>>>> <xsl:template name="parents">
>>>> <xsl:for-each select="ancestor-or-self::*">
>>>> <xsl:value-of select="concat(name(),'/')" />
>>>> </xsl:for-each>
>>>> <xsl:value-of select="." />
>>>> </xsl:template>
>>>> ------------------------ END ---------------------------
>>>>
>>>> And I got:
>>>>
>>>> ------------------------ BEGIN -------------------------
>>>> ==[
>>>> #[Comprobante/Emisor/SDF531211GE2]#
>>>> #[Comprobante/Receptor/SEFT760217GA1]#
>>>> ]==
>>>> ------------------------ END ---------------------------
>>>>
>>>> So something like "/Comprobante/Receptor/_at_rfc" should work.
>>>>
>>>> Am I right?
>>>>
>>>> Any ideas about why it isn't working?
>>>>
>>>> I'm very very thankful for the pointers I always get from this mailing
>>>> list.
>>>>
>>>> Ely.
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>