简体   繁体   中英

Replacing placeholders with sequence numbers in input XML using XSLT 2.0

I have input XML like Below:

<parent>
  <data>
    <text>1.This is sample text1.</text>
  </data>
  <data>
    <text>This is sample text without number.</text>
  </data>
  <data>
    <text>&lt;n&gt;.This is sample text2.</text>
  </data>
  <data>
    <text>&lt;n&gt;.This is sample text3.</text>
  </data>
  <data>
    <text>&lt;n&gt;.This is sample text4.</text>
  </data>
</parent>

And desired output xml is as below:

<parent>
  <data>
    <text>1.This is sample text1.</text>
  </data>
  <data>
    <text>This is sample text without number.</text>
  </data>
  <data>
    <text>2.This is sample text2.</text>
  </data>
  <data>
    <text>3.This is sample text3.</text>
  </data>
  <data>
    <text>4.This is sample text4.</text>
  </data>
</parent>

I need to replace <n> with sequence numbers starting from 2 with solution in XSLT 2.0. I tried using for-each with replace function and template along with replace function. But in template I won't be getting position() value. Could you please guide me in how I can achieve this?

I need to replace <n> with sequence numbers starting from 2

How about:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="text[starts-with(., '&lt;n&gt;')]">
    <xsl:variable name="n">
        <xsl:number count="text[starts-with(., '&lt;n&gt;')]" level="any"/>
    </xsl:variable>
    <xsl:copy>
        <xsl:value-of select="$n + 1" />    
        <xsl:value-of select="substring-after(., '&lt;n&gt;')" />   
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Not strictly answering the question, but I think this belongs here.

Instead of working on strings, you may also want to consider working on XML.

If you change your XML to:

<parent>
  <data>
    <text><n />. This is sample text1.</text>
  </data>
  <data>
    <text>This is sample text without number.</text>
  </data>
  <data>
    <text><n />. This is sample text2.</text>
  </data>
  <data>
    <text><n />. This is sample text3.</text>
  </data>
  <data>
    <text><n />. This is sample text4.</text>
  </data>
</parent>

(Note: I use <n /> instead of <n> so that the XML is valid).

Then you can use the following XSL stylesheet, which in my opinion is more in the "XML/XSL philosophy".

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

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

  <xsl:template match="n">
    <xsl:value-of select="count(preceding::n) + 1" />
  </xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM