Forenindex » Programme » Print/Bildbearbeitung » Adobe InDesign » XSLT für XML in Tabelle umbauen

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

23. Sep 2014, 13:42
Bewertung:

gelesen: 6294

Beitrag als Lesezeichen
Hallo!

Ich steh gerade mal wieder auf dem Schlauch und kann nirgendswo eine Dokumentation für XSLT finden oder ein Beispiel hierfür. Auch hier hab ich nichts gefunden was dem irgendwie Nahe kommt, dabei sollte es doch ganz einfach sein:

Folgende XML-Datei:
Code
<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns="com.cisag.app.general.obj.ItemPropertyCollection" xsi:schemaLocation="com.cisag.app.general.obj.ItemPropertyCollection ItemPropertyCollection.xsd" created="2014-09-19T08:35:20.798Z" locale="en-US-XMLSchemaCompliant" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" nlsMode="SINGLE_LANGUAGE" dateTimeMode="COMPACT">

<ItemPropertyCollection xmlns="com.cisag.app.general.obj.ItemPropertyCollection">
<Item>
<description>Bezeichnung</description>
</Item>
<Classification>
<description>Name</description>
<path>Nummer</path>
</Classification>
<Values>
<ANSCHLUSS>
<value>0</value>
</ANSCHLUSS>
</Values>
</ItemPropertyCollection>

</root>


soll via einer XSLT beim Import in Indesign in folgende Form gebracht werden:

Code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<root xmlns="com.cisag.app.general.obj.ItemPropertyCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="com.cisag.app.general.obj.ItemPropertyCollection ItemPropertyCollection.xsd" created="2014-09-19T08:35:20.798Z" locale="en-US-XMLSchemaCompliant" nlsMode="SINGLE_LANGUAGE" dateTimeMode="COMPACT">

<Textabschnitt>&#8233;
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="5" aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2"><Item><description>Bezeichnung</description></Item></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888"><Classification><description>Name</description></Classification></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><Classification><path>Nummer</path></Classification></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888">Anschluss</Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><Values><ANSCHLUSS><value>0</value></ANSCHLUSS></Values></Zelle>
</ItemPropertyCollection>&#8233;</Textabschnitt></root>


ItemPropertyCollection ist das sich immer wiederholende Element

Jetzt hätte ich gedacht das die XSLT so aussehen muss:
Code
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
exclude-result-prefixes="xs">

<xsl:output method = "xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="root">
<xsl:for-each select="root/ItemPropertyCollection">
<ItemPropertyCollection xmlns="com.cisag.app.general.obj.ItemPropertyCollection">


<xsl:element name="textcontainer">
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="5" aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2"><xsl:copy-of select="Item/description"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888"><xsl:copy-of select="Classification/description"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><xsl:copy-of select="Classification/path"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888">Anschluss</Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851">
<xsl:copy-of select="Values/ANSCHLUSS/value"/>
</Zelle>

</ItemPropertyCollection>

</xsl:element>


</ItemPropertyCollection>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Funktioniert nur leider nicht.
"Die Daten konnten icht in die Ausgabe geschrieben werden." heisst es nur. Womit kann man sowas denn debuggen? Oder wo ist der Fehler?

Vielen Dank für jeden Tipp!

Markus

XSLT für XML in Tabelle umbauen

drerol74
Beiträge gesamt: 507

24. Sep 2014, 13:14
Bewertung:

gelesen: 6230

Beitrag als Lesezeichen
Hallo Markus,

du hast da ein Problem mit dem Namensraum/Präfix. Du definierst in deiner XML-DAtei einen Standard-Namensraum. XPath-Ausdrücke benötigen dann aber (zumindest in XSLT 1.0) ein Präfix für die Auswertung.

Versuch es mal so: Du ordnest dem Namensraum ein eigenes Präfix zu, also zu Beginn der Transformation z.B.: xmlns:my="com.cisag.app.general.obj.ItemPropertyCollection"

Dann stellst du allen Elementen in den XPath-Ausdrücken ein my: voran:

Code
<xsl:template match="my:root">  
<xsl:for-each select="my:ItemPropertyCollection">

... <xsl:copy-of select="my:Item/my:description"/> ...

usw.


Antwort auf: Womit kann man sowas denn debuggen? Oder wo ist der Fehler?


Mit »Kernow« etwa kannst du die Transformation testen oder mit »XPath Checker« als Addon für Firefox den XPath-Ausdruck überprüfen.

Schöne Grüße
Roland

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

25. Sep 2014, 11:30
Bewertung:

gelesen: 6164

Beitrag als Lesezeichen
Hallo Roland!
Super! Das funktioniert schonmal! Vielen Dank!
Nur klappt es nur wenn in der XML nur ein Datensazu drin ist. Wenn da viele drin sind sagt es jetzt
Zitat DOM-Transformationsfehler: Ungültige Hierarchie.

Obwohl die XML genausoaussieht wie die andere. nur eben mit mehr ItemPropertyCollection Enträgen.
Was mach ich da jetzt falsch? Dafür hab ich doch extra das for-each drin ...
Die XSL sieht jetzt so aus:
Code
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
xmlns:my="com.cisag.app.general.obj.ItemPropertyCollection"
exclude-result-prefixes="xs">

<xsl:output method = "xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="my:root">
<xsl:for-each select="my:ItemPropertyCollection">
<ItemPropertyCollection xmlns="com.cisag.app.general.obj.ItemPropertyCollection">


<xsl:element name="textcontainer">
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="5" aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2"><xsl:copy-of select="my:Item/my:description"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888"><xsl:copy-of select="my:Classification/my:description"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><xsl:copy-of select="my:Classification/my:path"/></Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888">Anschluss</Zelle>

<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851">
<xsl:copy-of select="my:Values/my:ANSCHLUSS/my:value"/>
</Zelle>

</ItemPropertyCollection>

</xsl:element>


</ItemPropertyCollection>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Dank und Gruss
Markus

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

25. Sep 2014, 11:43
Bewertung:

gelesen: 6158

Beitrag als Lesezeichen
Nachtrag:
Als zu platzierendes Element wird ja jetzt immer "textcontainer" angezeigt.
Wie kann ich dem sagen das er bitte da dann hinschreibt z.B. das Ergebnis von <xsl:copy-of select="my:Item/my:description"/>, damit man auch weiss was es ist?

Markus

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

25. Sep 2014, 13:15
Bewertung:

gelesen: 6118

Beitrag als Lesezeichen
Ich hab das jetzt auch mal mit Kernow getestet, da läuft das gut durch. Auch wenn mich stört das die Namespaceeinträge bei jedem Elemt wieder reingeschrieben werden.
Wenn man diese Datei dann in Indesign laden will kommt dasnn der Fehler das er sich da stört wo der zweite textcontainer anfängt ... Irgendwelche Ideen?

Die transformierte XML sieht jetzt so aus
Code
<?xml version="1.0" encoding="UTF-8"?> 
<textcontainer>
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
xmlns:my="com.cisag.app.general.obj.ItemPropertyCollection"
aid:table="table"
aid:trows="5"
aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2">
<description xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Bezeichnung</description>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="180.70393700785888">
<description xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Name</description>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="285.5952755903851">
<path xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Nummer</path>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="180.70393700785888">Anschluss</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="285.5952755903851">
<value xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">0</value>
</Zelle>
</ItemPropertyCollection>
</textcontainer>
<textcontainer>
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
xmlns:my="com.cisag.app.general.obj.ItemPropertyCollection"
aid:table="table"
aid:trows="5"
aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2">
<description xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Bezeichnung2</description>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="180.70393700785888">
<description xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Name2</description>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="285.5952755903851">
<path xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Nummer2</path>
</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="180.70393700785888">Anschluss</Zelle>
<Zelle aid:table="cell"
aid:crows="1"
aid:ccols="1"
aid:ccolwidth="285.5952755903851">
<value xmlns="com.cisag.app.general.obj.ItemPropertyCollection"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1</value>
</Zelle>
</ItemPropertyCollection>
</textcontainer>

XSLT für XML in Tabelle umbauen

drerol74
Beiträge gesamt: 507

26. Sep 2014, 00:33
Bewertung:

gelesen: 6038

Beitrag als Lesezeichen
Hallo Markus,

fehlendes Root-Element ...

Code
<?xml version="1.0" encoding="UTF-8"?>  

<root>
<textcontainer>
...


Schöne Grüße
Roland

XSLT für XML in Tabelle umbauen

drerol74
Beiträge gesamt: 507

26. Sep 2014, 00:37
Bewertung:

gelesen: 6037

Beitrag als Lesezeichen
Antwort auf: Nachtrag:
Als zu platzierendes Element wird ja jetzt immer "textcontainer" angezeigt.
Wie kann ich dem sagen das er bitte da dann hinschreibt z.B. das Ergebnis von <xsl:copy-of select="my:Item/my:description"/>, damit man auch weiss was es ist?


Meinst du als Attribut? Bring bitte ein Beispiel dazu, was du hier wo haben möchtest?

XSLT für XML in Tabelle umbauen

mx
Beiträge gesamt: 161

26. Sep 2014, 17:23
Bewertung:

gelesen: 5944

Beitrag als Lesezeichen
Hallo Markus,

folgendes XSLT sollte in etwa passen:

Code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"
xmlns:ipc="com.cisag.app.general.obj.ItemPropertyCollection"
exclude-result-prefixes="xs ipc">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

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

<xsl:template match="ipc:root">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ipc:ItemPropertyCollection">
<Textabschnitt>
<ItemPropertyCollection xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="5" aid:tcols="2">
<Zelle aid:table="cell" aid:crows="1" aid:ccols="2"><xsl:apply-templates select="ipc:Item"/></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888"><Classification><xsl:apply-templates select="ipc:Classification/ipc:description"/></Classification></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><Classification><xsl:apply-templates select="ipc:Classification/ipc:path"/></Classification></Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="180.70393700785888">Anschluss</Zelle>
<Zelle aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="285.5952755903851"><xsl:apply-templates select="ipc:Values"/></Zelle>
</ItemPropertyCollection>
</Textabschnitt>
</xsl:template>

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

<xsl:template match="ipc:description | ipc:path | ipc:value">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>



Viele Grüße
Jo

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

7. Okt 2014, 15:37
Bewertung:

gelesen: 5498

Beitrag als Lesezeichen
Hallo Roland!
Ah, Danke! Augen auf! ...
Markus

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

7. Okt 2014, 15:40
Bewertung:

gelesen: 5497

Beitrag als Lesezeichen
Hi Roland

Ja genau.
Das ist ja jetzt völlig "werte-los"
also bei 100 Elementen steht halt in der Strukturübersicht hundertmal textcontainer und ich weiss nicht um welchen "Artikel" es sich handelt. Dafür gibt's ja diese schlaue Werte-Vorschau, und genau die hätte ich an der Stelle gerne damit ich das richtige auswählen kann.

Markus

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

7. Okt 2014, 15:56
Bewertung:

gelesen: 5491

Beitrag als Lesezeichen
Hi Jo!

Vielen Dank!
Da kommt jetzt dasselbe raus wie bei dem was ich da zusammengeschustert habe.
Was ich allerdings nicht verstehe ist warum mir auch bei bestimmten importoptionen neben dem Wert von z.B. <xsl:apply-templates select="ipc:Item"/> er noch hinschreibt "Item". Ist das ein normales Verhalten?

Markus

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

7. Okt 2014, 16:19
Bewertung:

gelesen: 5462

Beitrag als Lesezeichen
Und eine letzte Frage hierzu:
Kann man mit dem XSL auch Änderungen vornehmen lassen im Inhalt?
Beispiel: In einem Feld "XY" steht immer der Wert "10" oder "20" und in der InDesign-Datei soll bei "10" aber "grün" stehen und bei "20" dann "blau". Und das ganze für alle möglichen anderen Felder.
Geht das?

Danke,
Markus

XSLT für XML in Tabelle umbauen

mx
Beiträge gesamt: 161

7. Okt 2014, 17:36
Bewertung:

gelesen: 5417

Beitrag als Lesezeichen
Hallo Markus,

die Werte in ipc:value könntest du z.B. ändern, wenn Du folgendes Template noch ergänzt:

Code
<xsl:template match="ipc:value" priority="2"> 
<xsl:choose>
<xsl:when test=". = '10'">
<xsl:value-of select="'grün'" />
</xsl:when>
<xsl:when test=". = '20'">
<xsl:value-of select="'blau'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


Viele Grüße
Jo

XSLT für XML in Tabelle umbauen

Markus76
Beiträge gesamt: 340

8. Okt 2014, 15:44
Bewertung:

gelesen: 5312

Beitrag als Lesezeichen
Hi Jo!
Vielen Dank! Das werde ich gleich mal testen!

Markus

XSLT für XML in Tabelle umbauen

drerol74
Beiträge gesamt: 507

8. Okt 2014, 22:41
Bewertung:

gelesen: 5263

Beitrag als Lesezeichen
Hallo Markus

Zitat Das ist ja jetzt völlig "werte-los"
also bei 100 Elementen steht halt in der Strukturübersicht hundertmal textcontainer und ich weiss nicht um welchen "Artikel" es sich handelt.


so gibst du dem Element <textcontainer> ein Attribut mit:

Code
<xsl:element name="textcontainer">  
<xsl:attribute name="desc"><xsl:value-of select="my:Item/my:description" /></xsl:attribute>

...


(xsl:attribute muss gleich nach dem Start-TAg von <textcontainer> eingefügt werden)

Schöne Grüße
Roland