0

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.

2
  • 1
    Read up on attribute value templates Commented Jan 29, 2014 at 20:49
  • 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}" /> Commented Jan 29, 2014 at 21:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I tried both the suggestion, and it works. Exactly what i was looking for, and IanRoberts reference on attribute value elements [your second suggestion] made me understand more on its usage. To your extra note on adding the id attribute to the <newbook> element, i understand what you are saying, but in my real scenario, the attribute name is not id, it is a href for custom purpose. The purpose of <mynewtag> element in this case is not to hold the id attribute, it has its own importance in the real context. Thanks , and I appreciate your help
It was not attribute value elements. I meant to say attribute value templates.

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.