开发者

Template Match and document

Can I do something like that to display the value of the node?

<!-- plop.xml : -->
<?xml version="1.0"?>
<root>
      &开发者_Python百科lt;node1>hello</node1>
</root>

<xsl:template name="my_template" match="document('plop.xml')/root" >
       <xsl:value-of select="node1"/>
</xsl:template>

 <xsl:call-template name="my_template"></xsl:call-template>

I want to create special template that will only affect one xml.

I work on asp .net 2.0 C# XslCompiledTransform processor.


No, you can't do it like that. (EDIT: In XSLT 1.0 at least, see @Alejandro's answer for a way to do it in XSLT 2.0.)

An XML node has no concept of the file it had been stored in, since XML does not necessarily correspond to an actual file in the first place. Therefore you can't write a template that only matches nodes in a certain file.

You can declare a parameter,

<xsl:param name="fileName" select="''" />

fill it from your C# program with a value of your choice, and then make the XSLT program behave differently based on the value of that parameter:

<xsl:template match="root">
  <xsl:choose>
    <xsl:when test="$fileName = 'plop.xml'">
      <!-- do something -->
    </xsl:when>
    <xsl:otherwise>
      <!-- do something else -->
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


In XSLT/XPath 2.0 you can:

<xsl:template match="/root[document-uri(.) eq resolve-uri('plop.xml',.)]">
        <xsl:value-of select="node1"/>
</xsl:template> 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜