0
votes

For my input XML, I have written the XSLT , But I cannot make the XSLT to generate the <mynewtag> correctly. Please help.

XML input:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book.child.1>
        <title>charithram</title>
        <author>sarika</author>
    </book.child.1>
    <book.child.2>
        <title>doublebell</title>
        <author>psudarsanan</author>
    </book.child.2>
</books>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<newbooks>
   <newbook>
      <mynewtag id="book1" />
      <title>charithram</title>
      <author>sarika</author>
   </newbook>
   <newbook>
      <mynewtag id="book2" />
      <title>doublebell</title>
      <author>psudarsanan</author>
   </newbook>
</newbooks>

XSLT that I tried: [I understand the syntax is incorrect for <mynewtag> ]. But I don't know to fix it to get the desired output.

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

    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />

    <xsl:template match="/">
        <newbooks>
            <xsl:for-each select="books/child::*">
                <newbook>
                    <mynewtag id="book<xsl:number value='position()' format='1' />" /> 
                    <title>
                        <xsl:value-of select="title" />
                    </title>

                    <author>
                        <xsl:value-of select="author" />
                    </author>
                </newbook>
            </xsl:for-each>
        </newbooks>
    </xsl:template>
</xsl:stylesheet>

Trying in Online XSLT transformer , http://www.freeformatter.com/xsl-transformer.html

I tried assigning the position to a variable, but still I face the same problem of not knowing how to append it with the attribute value book .

<xsl:variable name="cnt">
  <xsl:number value='position()' format='1' />
</xsl:variable>
<xsl:value-of select = "$cnt" />

Note: If I remove the <xsl:number value='position()' format='1' /> from XSL, then the syntax is correct, but then I won't be able to generate book1 book2 etc as <mynewtag> attribute values.

Please help.

Added: <mynewtag> is a required element. It is like any other XML element like <title> that is required in output. It is not an element just to hold the attribute id. Sorry if there is a confusion on this.

Adding Solution here, from the answers obtained to summarize:

<mynewtag>
    <xsl:attribute name="id">
        <xsl:text>book</xsl:text>
        <xsl:number value='position()'/>
    </xsl:attribute>
</mynewtag>

or shortly:

<mynewtag id="book{position()}" />" 

or

  <newbook> 
        <xsl:variable name="cnt">
          <xsl:number value='position()' format='1' />
        </xsl:variable>
            <mynewtag id="book{$cnt}" />
             ..........

Also the attribute value templates that IanRoberts mentioned.

1
Thank you @IanRoberts for providing reference on attribute value templates. Though michael.hor257k has answered my query completely, this extra reference helped me to look into it more. So I can achieve the result by a different way too . Thanks! <mynewtag id="book{$cnt}" />spiderman

1 Answers

2
votes

You can't place a tag inside another tag. Try either:

<mynewtag>
    <xsl:attribute name="id">
        <xsl:text>book</xsl:text>
        <xsl:number value='position()'/>
    </xsl:attribute>
</mynewtag>

or shortly:

<mynewtag id="book{position()}" />" 

ADDED:
Not directly related to your question, but I believe the id attribute should be applied to the parent <newbook> element, instead of creating an artificial child element to hold it.