开发者

Can I re-use XSL templates with unique XML content?

Maybe there is a better way of describing my problem...

Here is an example of what I would like to do:

Take this:

<inc_3c>
    <Title>This is 3C</Title>
    <Subtitle>Lorem ipsum dolor consectetur adipiscing elit. Fusce mattis consequat malesuada.</Subtitle>
    <Description>Consectetur adipiscing elit. Fusce mattis consequat malesuada. Nullam vel justo a dui pellentesque euismod. Integer eu pulvinar dolor.</Description>
    <Class>priceWrapper</Class>
    <Price>$00.00</Price>
    <Url>Url</Url>
    <Image>apple-tv.png</Image>
</inc_3c>

and copy it like so:

<inc_3c-copy>
    <Title>This is 3C</Title>
    &l开发者_StackOverflowt;Subtitle>Lorem ipsum dolor consectetur adipiscing elit. Fusce mattis consequat malesuada.</Subtitle>
    <Description>Consectetur adipiscing elit. Fusce mattis consequat malesuada. Nullam vel justo a dui pellentesque euismod. Integer eu pulvinar dolor.</Description>
    <Class>priceWrapper</Class>
    <Price>$00.00</Price>
    <Url>Url</Url>
    <Image>apple-tv.png</Image>
</inc_3c-copy>
  • inc_3c = would populate content on page#1
  • inc_3c-copy = would populate content on page#2

might even be a scenario where inc_3c and inc_3c-copy live on the same page.

hopefully I wouldn't need to create another XSL page.

this would have to be non-coder proof.


Learn about using the identity rule/template and overriding it. This alone is the most fundamental and powerful XSLT design pattern.

In your case we use this design pattern to override just the top element in the following way:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="/inc_3c">
  <inc_3c-copy>
   <xsl:apply-templates/>
  </inc_3c-copy>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<inc_3c>
    <Title>This is 3C</Title>
    <Subtitle>Lorem ipsum dolor consectetur adipiscing elit. Fusce mattis consequat malesuada.</Subtitle>
    <Description>Consectetur adipiscing elit. Fusce mattis consequat malesuada. Nullam vel justo a dui pellentesque euismod. Integer eu pulvinar dolor.</Description>
    <Class>priceWrapper</Class>
    <Price>$00.00</Price>
    <Url>Url</Url>
    <Image>apple-tv.png</Image>
</inc_3c>

the wanted, corresct result is produced:

<inc_3c-copy>
   <Title>This is 3C</Title>
   <Subtitle>Lorem ipsum dolor consectetur adipiscing elit. Fusce mattis consequat malesuada.</Subtitle>
   <Description>Consectetur adipiscing elit. Fusce mattis consequat malesuada. Nullam vel justo a dui pellentesque euismod. Integer eu pulvinar dolor.</Description>
   <Class>priceWrapper</Class>
   <Price>$00.00</Price>
   <Url>Url</Url>
   <Image>apple-tv.png</Image>
</inc_3c-copy>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜