views:

79

answers:

2

How can I dynamically specify the href value in the line

<?xml-stylesheet type="text/xsl" href="first.xsl"?>

in an xml?

I would like to substitute "first.xsl" dynamically.

Thanks for your help. ================== Apologies for messing this up/making it difficult for you to help ====

My XML is:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="first.xsl"?>
<WinData records="1" fields="4">
<record id="1">
    <Name type="82">January</Name>
    <Dept type="323">19</Dept>
    <InceptionDate type="82">01/01/2010</InceptionDate>
    <Salary type="645">3729.71</Salary>
</record>
<!-- Created from D:\AJAY\C#\VS2010\SBWA\XML -->
</WinData>

The XSL is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<xsl:template match="/">
  <html>
  <body>
  <h2>Sub Selection of Data</h2>
    <table border="0">
      <tr bgcolor="#9acd32">
        <th>Name</th>
        <th>Dept</th>
        <th>InceptionDate</th>
        <th>Salary</th>
      </tr>
      <xsl:for-each select="WinData/record" >
      <xsl:sort select="./Name"/>
      <xsl:sort select="./Dept"/>
      <xsl:if test="Salary>100"> 
      <tr>
        <td><xsl:value-of select="Name"/></td>
        <td><xsl:value-of select="Dept"/></td>
        <td><xsl:value-of select="InceptionDate"/></td>
        <td><xsl:value-of select="Salary"/></td>
      </tr>
      </xsl:if>   
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

My specific issues/objectives:

  1. How can I incorporate the code snippet offerred by iHeartGreek?
  2. Is there a way to pass the 'replacement' xsl (newLink.xsl in the snippet) as a parameter?
A: 

You could use an XSLT document to transform your XML data.

Use: (let's say the node is called "link")

<xsl:template match="link[.='first.xsl']">
    <xsl:element name="link">
      <xsl:text>newLink.xsl</xsl:text>
    </xsl:element>
</xsl:template>
developer
Thanks iHeartGreek. I need some more help, please. My XML (first.xml) looks like:<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="first.xsl"?><WinData records="1" fields="4"><record id="1"> <Name type="82">January</Name> <Dept type="323">19</Dept> <InceptionDate type="82">01/01/2010</InceptionDate> <Salary type="645">3729.71</Salary></record><!-- Created from D:\AJAY\C#\VS2010\SBWA\XML --></WinData>If I want to use the alternative newLink.xsl, where/how do I incorporate your snippet? In first.xml or first.xsl?
aa2e72e
@aa2e72e: Firstly, it is very difficult to read that much code in a comment, please edit your question if you want to show more detail about the XML code.
developer
@aa2e72e: Secondly, because you hadn't had your question formatted properly, I assumed the text you wanted to replace was in a node called "link", so therefore an exact copy/paste of my solution will not work for you. But it gives you a starting point. To answer your question, my solution would require creating a new XSLT document to do the transformation. It sounds like you haven't worked with XSLT before (also known as just XSL)?
developer
iHeartGreek: You are right - I do not have more than 3 days experience of XSLT.marc_s: Thanks for the tip on editing/posting.
aa2e72e
+1  A: 

Two posible solutions.

1) Two pass transformation. You can output a PI like this:

<xsl:processing-instruction name="xml-stylesheet">
    <xsl:value-of select="concat('type=&quot;text/xsl&quot; href=&quot;',
                                 $whatever,
                                 '&quot;')"/>
</xsl:processing-instruction>

2) Use a "population" pattern (or "fill in blanks", as Dimitre calls), and a metadata URI to get the layout with fn:document(). Look this http://stackoverflow.com/questions/3417350/dynamically-decide-which-xsl-stylesheet-to-use

Note: The PI is part of the input tree. Whether this input tree is from a parsed document or built by DOM API or whatever means, for the XSLT processor there is only a "static" node of type "PI" with a "final" string value.

Alejandro
+1 great solution! :) My solution was submitted before he edited his question and I had to assume he just wanted to change a regular ol' node and not a processing instruction.. maybe I'll delete it.
developer