XSLT&;XPath:如何以最有效的方式更改XML文件?

XSLT&;XPath:如何以最有效的方式更改XML文件?,xml,xslt,xpath,Xml,Xslt,Xpath,我是XSLT和XPath的新手,所以请原谅我这个简单的问题 我有以下XML文件: <?xml version="1.0"?> <Configuration serviceName="Just Service" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"> <Page name="Books"> <Ins

我是XSLT和XPath的新手,所以请原谅我这个简单的问题

我有以下XML文件:

<?xml version="1.0"?>
    <Configuration serviceName="Just Service" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration">
      <Page name="Books">
        <Instances count="5" />
        <ConfigurationSettings>
          <Setting name="index" value="true" />
          <Setting name="number" value="value1" />
          <Setting name="bookstorage" value="value2"/>
        </ConfigurationSettings>
      </Page>
      <Page name="Magazines">
        <Instances count="7" />
        <ConfigurationSettings>
          <Setting name="index" value="false" />
          <Setting name="number" value="value1" />
          <Setting name="magazinestorage" value="value3"/>
        </ConfigurationSettings>
      </Page>
    </Configuration>

我只想更改以下值

值1-用于编号(两处); 值2-用于图书存储值3-对于magazinestorage

。。。并使所有其他内容保持不变

为此,我想使用msxsl.exe(Microsoft命令行实用程序)。您能给我一个XSLT样式表示例的提示吗?如何以最有效的方式使用XSLT处理初始XML文件

谢谢,
Rac

我担心这个用例,使用xslt,从头开始构建一个完整的新文档会更容易。或者,使用您选择的编程语言,您可以读取DOM树并仅更改这些变量(如果需要,我可以为Java展示这一点)


在其他用例中,您可以让原始xml通过处理。查看更多信息。

在XSLT中实现这一点的方法是使用一个默认模板,该模板只复制文档内容,例如:

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

然后将模板添加到样式表中,该模板将匹配要更改的特定节点。当这些节点匹配时,它们将覆盖上面的默认复制模板。例如,如果希望元素设置的每个数字属性的值为314,则应添加一个模板:

<xsl:template match="Setting/@number">
    <-- this copies in an attribute 'number' in place; with different contents -->
    <xsl:copy>314</xsl:copy>    
<xsl:template/>

314

这两个模板,加上您希望进行的任何其他替换,都将以任意顺序出现在样式表中。

下面是一个示例XSLT 1.0样式表,它使用三个参数和新值:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"
  exclude-result-prefixes="sc"
  version="1.0">

  <xsl:param name="p1" select="'foo'"/>
  <xsl:param name="p2" select="'bar'"/>
  <xsl:param name="p3" select="'foobar'"/>

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

  <xsl:template match="sc:Setting[@name = 'number']/@value">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="$p1"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="sc:Setting[@name = 'bookstorage']/@value">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="$p2"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="sc:Setting[@name = 'magazinestorage']/@value">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="$p3"/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

Karussell,谢谢你的回答。实际上,我不介意它是否是从头开始的新文档。msxsl.exe具有此选项。或者我几乎无法重定向输出。因此,让它成为新的文件。那么XSTLT在这种情况下会是什么样子呢?谢谢