Pass href dynamically in an XML file

2019-09-02 11:39发布

问题:

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">

<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?

回答1:

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 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.



回答2:

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>


标签: xml xslt