0

I know similar questions are already there but none of them seem to work for me.

So shortly, I have XML file with tag "Lokal" that in most cases does not appear but it should. Not making things easier: I also need to change a name of "Lokal" to let's say "Lokal_test". My goal is modify node name(if exists) or create it and rename (if does not exists). Data from XML will be imported to MS Access data so they need to match perfectly with table...

Sample XML:

<Dane>
  <InformacjeOWpisie>
    <DaneAdresowe>
      <AdresGlownegoMiejscaWykonywaniaDzialalnosci>
        <Budynek>3a</Budynek>
        <Wojewodztwo>podlaskie</Wojewodztwo>
      </AdresGlownegoMiejscaWykonywaniaDzialalnosci>
    </DaneAdresowe>
    </InformacjeOWpisie>
    <InformacjeOWpisie>
      <DaneAdresowe>
        <AdresGlownegoMiejscaWykonywaniaDzialalnosci>
          <Budynek>8r</Budynek>
          <Lokal>2</Lokal>
          <Wojewodztwo>mazowieckie</Wojewodztwo>
        </AdresGlownegoMiejscaWykonywaniaDzialalnosci>
      </DaneAdresowe>
      </InformacjeOWpisie>
</Dane>

Desired output:

<Dane>
  <InformacjeOWpisie>
    <DaneAdresowe>
      <AdresGlownegoMiejscaWykonywaniaDzialalnosci>
        <Budynek>3a</Budynek>
        <Lokal_test/>
        <Wojewodztwo>podlaskie</Wojewodztwo>
      </AdresGlownegoMiejscaWykonywaniaDzialalnosci>
    </DaneAdresowe>
    </InformacjeOWpisie>
    <InformacjeOWpisie>
      <DaneAdresowe>
        <AdresGlownegoMiejscaWykonywaniaDzialalnosci>
          <Budynek>8r</Budynek>
          <Lokal_test>2</Lokal_test>
          <Wojewodztwo>mazowieckie</Wojewodztwo>
        </AdresGlownegoMiejscaWykonywaniaDzialalnosci>
      </DaneAdresowe>
      </InformacjeOWpisie>
</Dane>

This question(XSLT: create node if not exists seemed to be the awnser to my problems but when trying to use it does not work. Not sure why?

<xsl:template match="InformacjeOWpisie/DaneAdresowe/AdresGlownegoMiejscaWykonywaniaDzialalnosci/Lokal">
<Lokal_test>
    <xsl:apply-templates select="@*|node()" />
</Lokal_test>
</xsl:template>

EDIT:
When I get rid of parent Lokal_test dissapears. I use below code to say "bye bye" to parent:

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

2 Answers 2

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

    <xsl:template match="AdresGlownegoMiejscaWykonywaniaDzialalnosci/Budynek">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
            <xsl:choose>
                <xsl:when test="exists(following-sibling::Lokal)">
                    <Lokal_test>
                    <xsl:value-of select="following-sibling::Lokal"/>
                    </Lokal_test>
                </xsl:when>
                <xsl:when test="not(following-sibling::Lokal)">

                    <xsl:element name="Lokal_test"/>
            </xsl:when>
            </xsl:choose>


    </xsl:template>
    <xsl:template match="Lokal"/>
Sign up to request clarification or add additional context in comments.

1 Comment

It works and it does not at the same time. When pasting in online test tool it works. However Access says: exists(following-sibling::Lokal) is not a valid function of XSLT or XPath :(
0

You approach was right, but incomplete. You only created the new Local_test element.

So try these two templates in combination with the indentity template:

<!-- Handles the replacement of the 'Lokal' element -->
<xsl:template match="AdresGlownegoMiejscaWykonywaniaDzialalnosci/Lokal">
    <Lokal_test>
        <xsl:apply-templates select="node()|@*" />
    </Lokal_test>
</xsl:template>

<!-- Creates a new 'Lokal_test' element if no 'Lokal' element exists -->
<xsl:template match="AdresGlownegoMiejscaWykonywaniaDzialalnosci[not(Lokal)]">
    <xsl:copy>
        <xsl:apply-templates select="node()/following-sibling::Wojewodztwo/preceding-sibling::*|@*" />     <!-- Copy nodes before 'Wojewodztwo' -->
        <Lokal_test />
        <xsl:apply-templates select="Wojewodztwo|Wojewodztwo/following-sibling::*|@*" />     <!-- Copy nodes after 'Wojewodztwo' (including) -->
    </xsl:copy>
</xsl:template> 

The second template puts the Lokal_test element before the Wojewodztwo element and copies the surrounding nodes.

7 Comments

thank you it does seem to work. However pasted to my code it does not. In other words, it works alone but not with the rest of the code. Plus I cannot seem to put in the right place - it pops up at the end not in the middle where I want to...
I updated my answer so that the new Lokal_test appears before the Wojewodztwo tag. What exactly does not work with your full code?
Alright, I managed to find the problem. When I try to get rid of parent (AdresGlownegoMiejscaWykonywaniaDzialalnosci) to newly created Lokal_test it disappears after transformation. Parent is not needed in my code but Lokal_test is important.
You can remove/not copy the AdresGlownegoMiejscaWykonywaniaDzialalnosci element by removing the xsl:copy from the second template. How does your full desired output look like now?
Tried to isolate the problem but it does not look so easy. Desired output has only: <Dane>, <InformacjeOWpisie>, <Budynek>, <Lokal_test> and <Wojewodztwo> tags in order and hierarchy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.