/ / Transformacja XSLT do attibute - xml, xslt

Transformacja XSLT do attibute - xml, xslt

Mam następujące pytanie dotyczące XSLTtransform - potrzebuję przekształcić plik XML uwzględniając wartości atrybutów we wszystkich węzłach nadrzędnych aż do katalogu głównego. Więc mając taki kod (jako atrybut XY użyj wartości XY):

<Layout XY="40,20">
<Layout XY="0,20">
<Circle OffsetX="0"/>
</Layout>
<Circle OffsetX="6" />
<Layout XY="100,20">
<Circle OffsetX="0"/>
<Layout XY="200,20">
<Circle OffsetX="5"/>
</Layout>
</Layout>
</Layout>

Potrzebuję wyjścia

<Layout XY="40,20">
<Layout XY="0,20">
<Circle OffsetX="40"/>
</Layout>
<Circle OffsetX="46" />
<Layout XY="100,20">
<Circle OffsetX="140"/>
<Layout XY="200,20">
<Circle OffsetX="345"/>
</Layout>
</Layout>
</Layout>

Próbowałem użyć transformacji XSLT przy użyciu szablonu takiego jak to:

<!-- Copy template -->

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!-- Change values -->

<xsl:template match="Circle/@OffsetX">
<xsl:param name="newOffsetX" select="substring-before(../../@XY,",")"/>
<xsl:apply-templates select="@*|node()"/>
<xsl:attribute name="OffsetX">
<xsl:value-of select=".+$newOffsetX"/>
</xsl:attribute>
</xsl:template>

ale jest to rozwiązanie tylko na wyższy poziom. Czy można w ogóle dokonać takiej transformacji tylko przy użyciu XSLT?

Odpowiedzi:

1 dla odpowiedzi № 1

Jeśli pracujesz z XSLT 1.0, potrzebujesz szablonu, który rekursywnie przechodzi przez drzewo węzłów:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@XY" mode="sumXOffset" name="SumXOffset">
<xsl:param name="currentValue"
select="substring-before(., ",")" />

<xsl:variable name="sum">
<xsl:apply-templates select="(../ancestor::*/@XY)[last()]" mode="sumXOffset" />
</xsl:variable>

<xsl:value-of select="$currentValue +
concat("0", $sum)" />
</xsl:template>

<xsl:template match="@OffsetX">
<xsl:attribute name="{name()}">
<xsl:call-template name="SumXOffset">
<xsl:with-param name="currentValue" select="." />
</xsl:call-template>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Po uruchomieniu na przykładowym kodzie XML wynik jest następujący:

<Layout XY="40,20">
<Layout XY="0,20">
<Circle OffsetX="40" />
</Layout>
<Circle OffsetX="46" />
<Layout XY="100,20">
<Circle OffsetX="140" />
<Layout XY="200,20">
<Circle OffsetX="345" />
</Layout>
</Layout>
</Layout>

Jeśli masz procesor XSLT 2.0, rozwiązanie jest o wiele prostsze:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@OffsetX">
<xsl:attribute name="{name()}">
<xsl:value-of select=". + sum(ancestor::*/@XY/substring-before(","))"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

1 dla odpowiedzi nr 2

Zrobiłbym to w ten sposób:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Circle/@OffsetX">
<xsl:variable name="previous-X">
<xsl:for-each select="ancestor::*[@XY]">
<x><xsl:value-of select="substring-before(@XY, ",")"/></x>
</xsl:for-each>
</xsl:variable>
<xsl:attribute name="OffsetX">
<xsl:value-of select=". + sum(exsl:node-set($previous-X)/x)"/>
</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

0 dla odpowiedzi № 3

Jest sposób na osiągnięcie tego w XSLT 1.0 bez szablonu rekursywnego (nie ma w tym nic złego!) I to jest przekazywanie całkowitej wartości z szablonu do szablonu jako parametru. Zasadniczo użyj newOffsetX również w szablonie tożsamości.

Wypróbuj ten XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:param name="newOffsetX" select="0" />
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:with-param name="newOffsetX" select="$newOffsetX + number(substring-before(concat("0", @XY, ","), ","))" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="@OffsetX">
<xsl:param name="newOffsetX" select="0" />
<xsl:attribute name="OffsetX">
<xsl:value-of select="$newOffsetX + number(.)" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Zabawny wyraz substring-before(concat("0", @XY, ","), ",") jest poradzić sobie z węzłami bez @XY atrybut. Kiedy @XY atrybut jest obecny, na przykład o wartości "40,20", "concat" zwraca "040,20", a więc liczba X jest nadal numerycznie taka sama. Kiedy @XY nie występuje, concat zwraca 0, i tak numer X to "0".