xsl-list
[Top] [All Lists]

Re: [xsl] Copy all attributes except except some

2014-06-19 04:38:53
I don’t have the original source any more and the problem is probably a bit underspecified, but here’s one solution.

Given this input

<doc>
  <source note="hello" noteAdditions="world" expert="HAHA"/>
</doc>

applying this stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">
  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="source/@note"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@note">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
      <xsl:apply-templates select="../@noteAdditions"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@noteAdditions">
    <xsl:text>&#xd;&#xa;</xsl:text>
    <xsl:value-of select="../@expert"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

yields

<doc note="hello&#xD;&#xA;HAHA:world"/>

This might give you an idea how to create your own solution.
Please note that in the stylesheet above, @expert will be discarded if there are no @noteAdditions. I don’t know whether that was you intention. This is what I meant to say by “underspecified”.

Gerrit


On 19.06.2014 11:19, Philipp Kursawe phil(_dot_)kursawe(_at_)gmail(_dot_)com 
wrote:
Thanks Gerrit, your useful tips made it work now.

One more thing:
In the source xml I hava the attribute "note" and "noteAdditions" that I
want to combine together joined by another attributes value:
<source note="hello" noteAdditions="world" expert="HAHA">
<result note="hello&#xD;&#xA;HAHA:world">


On Thu, Jun 19, 2014 at 7:27 AM, Imsieke, Gerrit, le-tex
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de 
<mailto:gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de>
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:



    On 19.06.2014 03:33, Philipp Kursawe phil(_dot_)kursawe(_at_)gmail(_dot_)com
    <mailto:phil(_dot_)kursawe(_at_)gmail(_dot_)com> wrote:

        The resulting XML should contain only 2 attributes from
        "InspectionExt"
        in case result="10" or "11" otherwise all attributes and
        "Obligations",
        "Defects" but no "Photo"
        Also when copying the Defects/Defect items, only those with
        "not(@reinspection='ok'" should be copied over. No "Defects" element
        should be created if nothing from source xml is copied.
        The resulting "Defect" element must not contain the "reinspection"
        attribute.

        That's what I came up with after your suggestions:
        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/__1999/XSL/Transform
        <http://www.w3.org/1999/XSL/Transform>"
        xmlns:xs="http://www.w3.org/__2001/XMLSchema
        <http://www.w3.org/2001/XMLSchema>"
        xmlns:fn="http://www.w3.org/__2005/xpath-functions
        <http://www.w3.org/2005/xpath-functions>">
            <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes"/>

            <xsl:template match="InspectionExt">
              <xsl:element name="ns2:__InspectionReportInput"
        namespace="http://__somenamespace <http://somenamespace>">
        <xsl:choose>
        <xsl:when test="@result eq '10' or @result eq '11'">


    In XPath 1, there is no eq operator. Use = instead.


        <xsl:copy-of select="@result"/>
        <xsl:copy-of select="@expert"/>
        </xsl:when>
        <xsl:otherwise>
                <xsl:copy-of select="@*[not(@signed)]"/>
                <xsl:for-each select="Defects">
                  <xsl:element name="Defects">
                    <xsl:for-each
        select="Defect[not(@__reinspection='ok')]">
                      <xsl:element name="Defect">
                        <xsl:copy-of select="@*[not(@reinspection)]__"/>


    You are selecting all attributes that don't have a reinspection
    attribute attached to them. Since no attribute carries other
    attributes, your predicate is always true. Use @*[not(name() =
    'reinspection')] instead.

    Gerrit



XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <-list/225679>
(by email <>)

--
Gerrit Imsieke
Geschäftsführer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>