将Xml Snippet作为参数传递给xslt

时间:2012-05-01 00:50:58

标签: c# xml xslt

我想解决的一个当前问题是通过xslt生成两个具有差异和相似性的xml文件的比较。

例如,第一个xml文件看起来像

<?xml version="1.0" encoding="utf-8" ?>
<Stats Date="2011-01-01">
  <Player Rank="1">
    <GP>39</GP>
    <G>32</G>
    <A>33</A>
    <PlusMinus>20</PlusMinus>
    <PIM>29</PIM>
    <PP>10</PP>
    <SH>1</SH>
    <GW>3</GW>
    <Shots>0</Shots>
    <ShotPctg>154</ShotPctg>
    <TOIPerGame>20.8</TOIPerGame>
    <ShiftsPerGame>21:54</ShiftsPerGame>
    <FOWinPctg>22.6</FOWinPctg>
  </Player>
</Stats>

第二个文件看起来像

<?xml version="1.0" encoding="utf-8" ?>
<Stats Date="2011-01-01">
  <Player Rank="2">
    <Name>John Smith</Name>
    <Team>NY</Team>
    <Pos>D</Pos>
    <GP>38</GP>
    <G>32</G>
    <A>33</A>
    <PlusMinus>15</PlusMinus>
    <PIM>29</PIM>
    <PP>10</PP>
    <SH>1</SH>
    <GW>4</GW>
    <Shots>0</Shots>
    <ShotPctg>158</ShotPctg>
    <TOIPerGame>20.8</TOIPerGame>
    <ShiftsPerGame>21:54</ShiftsPerGame>
    <FOWinPctg>22.6</FOWinPctg>
  </Player>
</Stats>

当我将第二个文件嵌入到xslt文件中时,输出按预期工作:

<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:param name="vrtfDoc2">
    <Stats Date="2011-01-01">
      <Player Rank="2">
        <Name>John Smith</Name>
        <Team>NY</Team>
        <Pos>D</Pos>
        <GP>38</GP>
        <G>32</G>
        <A>33</A>
        <PlusMinus>15</PlusMinus>
        <PIM>29</PIM>
        <PP>10</PP>
        <SH>1</SH>
        <GW>4</GW>
        <Shots>0</Shots>
        <ShotPctg>158</ShotPctg>
        <TOIPerGame>20.8</TOIPerGame>
        <ShiftsPerGame>21:54</ShiftsPerGame>
        <FOWinPctg>22.6</FOWinPctg>
      </Player>
    </Stats>
  </xsl:param>

  <xsl:variable name="vDoc2" select=
  "document('')/*/xsl:param[@name='vrtfDoc2']/*"/>

  <xsl:template match="node()|@*" name="identity">
    <xsl:param name="pDoc2"/>
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:with-param name="pDoc2" select="$pDoc2"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="*">
      <xsl:with-param name="pDoc2" select="$vDoc2"/>
    </xsl:apply-templates>

    -----------------------

    <xsl:apply-templates select="$vDoc2">
      <xsl:with-param name="pDoc2" select="/*"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Player/*">
    <xsl:param name="pDoc2"/>
    <xsl:if test=
   "not(. = $pDoc2/*/*[name()=name(current())])">
      <xsl:call-template name="identity"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="Name|Team|Pos" priority="20"/>
</xsl:stylesheet>

使用以下c#代码时:

private string Transform(string xml, string xml2, string xsl) {
            StringWriter writer = new StringWriter();
            XslCompiledTransform t = new XslCompiledTransform(true);
            XsltSettings settings = new XsltSettings(true, false);
            XmlTextReader xmlReader = new XmlTextReader(xml);

            XmlTextReader xslReader = new XmlTextReader(xsl);
            t.Load(xslReader, settings, null);

            t.Transform(xmlReader, null, writer);
            return writer.ToString();
        }

当我从xslt

中删除嵌入的xml时
<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:param name="vrtfDoc2" />

  <xsl:variable name="vDoc2" select=
  "document('')/*/xsl:param[@name='vrtfDoc2']/*"/>

  <xsl:template match="node()|@*" name="identity">
    <xsl:param name="pDoc2"/>
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:with-param name="pDoc2" select="$pDoc2"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="*">
      <xsl:with-param name="pDoc2" select="$vDoc2"/>
    </xsl:apply-templates>

    -----------------------

    <xsl:apply-templates select="$vDoc2">
      <xsl:with-param name="pDoc2" select="/*"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Player/*">
    <xsl:param name="pDoc2"/>
    <xsl:if test=
   "not(. = $pDoc2/*/*[name()=name(current())])">
      <xsl:call-template name="identity"/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="Name|Team|Pos" priority="20"/>
</xsl:stylesheet>

将c#方法更改为

private string Transform(string xml, string xml2, string xsl) {
            StringWriter writer = new StringWriter();
            XslCompiledTransform t = new XslCompiledTransform(true);
            XsltSettings settings = new XsltSettings(true, false);
            XmlTextReader xmlReader = new XmlTextReader(xml);

            XmlDocument doc1 = new XmlDocument();
            // populate as needed e.g.
            doc1.Load(xml2);

            XmlTextReader xslReader = new XmlTextReader(xsl);
            t.Load(xslReader, settings, null);

            //Pass parameter value to xslt from code
            XsltArgumentList argumentList = new XsltArgumentList();
            argumentList.AddParam("vrtfDoc2", "", doc1);
            t.Transform(xmlReader, argumentList, writer);
            return writer.ToString();
        }

我从变换得到一个空白输出,对于我的生活,我无法理解为什么。我已经使用调试器逐步完成了两个版本,并且参数值在两个场景中看起来都相同但是当参数传递版本命中xslt中的以下片段时,不会发生转换:

<xsl:apply-templates select="$vDoc2">
      <xsl:with-param name="pDoc2" select="/*"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Player/*">
    <xsl:param name="pDoc2"/>
    <xsl:if test=
   "not(. = $pDoc2/*/*[name()=name(current())])">
      <xsl:call-template name="identity"/>
    </xsl:if>
  </xsl:template>

任何帮助或建议都会受到很多限制。

1 个答案:

答案 0 :(得分:4)

问题在于此代码

  <xsl:param name="vrtfDoc2" />    

  <xsl:variable name="vDoc2" select=   
     "document('')/*/xsl:param[@name='vrtfDoc2']/*"/>

解析包含XSLT样式表的文件,找到globa; xsl:param name属性,其字符串值为"vrtfDoc2",并选择此xsl:param的子元素 - 但它没有子元素,因此$vDoc2的值为<xsl:variable name="vDoc2" select="$vrtfDoc2/*"/> 是空节点集。

<强>解决方案

仅使用

p

有关命名的说明

请重命名参数,因为其当前名称令人困惑和误导:

  1. 使用以v开头的名称作为变量以vrtfDoc2开头的参数和名称。

  2. pDoc2这样的名称通常意味着:此变量包含一个RTF(并且通常需要应用xxx:node-set()函数才能生成常规树来自它)。但是,在您的情况下情况并非如此。

  3. 因此,像{{1}}这样的参数名称更精确,信息更丰富。

相关问题