Jump to content

Converter XML para outro XML usando XSLT


Go to solution Solved by Big-D,

Recommended Posts

Posted

Boas!

Estou a fazer um projeto e surgiu-me um problema que me está a consumir faz horas. Como diz o título, eu quero converter um ficheiro XML para outro (com outro formato) através de um XSLT.

Consigo obter os dados, do XML original mas não consigo "mostrar" as tag's.

XML (original):

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href = "fotografiasXSLT1.xsl"?>
<Albuns xmlns="http://www.dei.isep.ipp.pt/lprog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dei.isep.ipp.pt/lprog fotografiasXSD.xsd">
<Album ID="3">
<Dono dono="Daniel Bastos" />
<NomeAlbum nomeAlbum="Artbook" />
<DataCriacao>2013-06-10</DataCriacao><!-->formato das datas: YYYY-MM-DD<-->
<DataModificacao>2013-08-16</DataModificacao>
<Descricao descricao="PhotoShop Covers" />
<Fotografias>
<Fotografia FotografiaID="9">
<NomeFotografia nomeFotografia="Cover Psicose 2.0 (com frase).png" />
<link>http://goo.gl/XQ1GJv</link>
<DataCriacao>2012-09-06</DataCriacao>
<HorasCriacao>17:39:59</HorasCriacao>
<Tipo ficheiro="png" />
<Autor autor="Daniel Bastos" />
<Localizacao>
    <Cidade>Maia</Cidade>
    <Pais>Portugal</Pais>
</Localizacao>
<Dimensoes>
    <Largura>1050</Largura>
    <Altura>1680</Altura>
</Dimensoes>
<TamanhoFotografia>1.34</TamanhoFotografia>
</Fotografia>
</Fotografias>
</Album>
</Albuns>

XSLT:

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

<xsl:template match="/Fotografias">
<xsl:copy>
<Fotografias>
<xsl:apply-templates select="Fotografia/*"/>
</Fotografias>
</xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/*">
    <xsl:copy>
    <Fotografia>
<xsl:value-of select="."/>
    </Fotografia>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/nomeFotografia">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/link">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/dataCriacao">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/horaCriacao">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/tipo">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/autor">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/localizacao">
    <xsl:copy>
<xsl:apply-templates select="localizacao/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/localizacao/*">
    <xsl:copy>
<xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Fotografia/localizacao/cidade">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/localizacao/pais">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/dimensoes">
    <xsl:copy>
<xsl:apply-templates select="dimensoes/*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/dimensoes/*">
    <xsl:copy>
<xsl:value-of select="."/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Fotografia/dimensoes/largura">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/dimensoes/altura">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Fotografia/tamanhoFotografia">
    <xsl:copy>
        <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', '')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

XML (output):

<?xml version="1.0" encoding="ISO-8859-1"?>



2013-06-10
2013-08-16




http://goo.gl/XQ1GJv
2012-09-06
17:39:59



    Maia
    Portugal


    1050
    1680

1.34


Será que me podem dar umas indicações sobre o que estou a fazer mal ou até mesmo cosnelhos para melhorar?

Desde já agradeço.

  • Solution
Posted

Consegui resolver o problema usando um xslt (muito) mais reduzido e usando o namespace, que me estava a esquecer. (Obrigado à mesma 🙂 )

<?xml version="1.0" encoding="ISO-8859-1"?><!-- DWXMLSource="fotografiasXML.xml" -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:x="http://www.dei.isep.ipp.pt/lprog">
<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>

<xsl:template match="x:Albuns">
	<xsl:apply-templates select="x:Album"/>
</xsl:template>

<xsl:template match="x:Album">
	<xsl:apply-templates select="x:Fotografias"/>
</xsl:template>

<xsl:template match="x:Fotografias">
	<xsl:copy-of select="x:Fotografia"/>
</xsl:template>

</xsl:stylesheet>

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.