1

I have a XSL/XML parser to produce html code.

The xml is like this.

<root>
  <a>
   <url> http://image.jpg </url>
   <x> 100 </x>
   <y> 200 </y>
   ...
  </a>
</root>

and the XST should be something like this. In style I want do define a background url using the value of xml node. How can I do it?

<xsl:template match="root">
   <xsl:for-each select="a">
    <div class="crop_image" style="background:url("<xsl:value-of select="url"/>") -<xsl:value-of select="x"/>px -<xsl:value-of select="y"/>px">"</div>
  </xsl:for-each>
</xsl:template>

Thanks

1 Answer 1

2

What you are looking for are "Attribute Value Templates", which allow you to write values from your XML directly into attributes

<xsl:template match="root">
  <xsl:for-each select="a">
    <div class="crop_image" style="background:url({url}) -{x}px -{y}px">"</div>
  </xsl:for-each>
</xsl:template>

The curly braces indicate an expression to be evaluated, rather than output literally, so {url} for example will be replaced with http://image.jpg in your output.

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

Comments

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.