XSLT: create node if does not exists

我怕爱的太早我们不能终老 提交于 2020-01-06 14:34:04

问题


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>

回答1:


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.




回答2:


<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"/>


来源:https://stackoverflow.com/questions/50456578/xslt-create-node-if-does-not-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!