XML 2 使用 XSLT 的 XML

发布于 2024-09-28 06:39:27 字数 1890 浏览 11 评论 0原文

我在 w3schools 学习了 XSLT 和 XPath,但这并不完全是我想要的。 只有有关转换 XSLT 2 HTML 的示例,这非常简单,但我需要 XML 2 XML 转换,并且找不到带有示例的好教程...我下载了 MSXSL.exe,但找不到有关使用它的示例转换 XML... 有人可以写一个样本吗? 我有像这样的customers.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Customers table from Northwind database-->
<Customers>
  <Customer CustomerID="ALFKI">
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>Germany</Country>
    <Orders>
      <Order OrderID="10643">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10692">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10702">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10835">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10952">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="11011">
        <ShipCity>Berlin</ShipCity>
      </Order>
    </Orders>
  </Customer>
  <Customer CustomerID="ANATR">
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <Country>Mexico</Country>
    <Orders>
      <Order OrderID="10308">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10625">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10759">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10926">
        <ShipCity>México D.F.</ShipCity>
      </Order>
    </Orders>
  </Customer>
</Customers>

并尝试生成另一个仅包含“国家/地区”节点的XML。 怎么写呢?

I learned about XSLT and XPath at w3schools,but it's not exactly what I wanted..
There are only examples about transforming XSLT 2 HTML, this is pretty easy,but I need a XML 2 XML transformation and can't find a good tutorial with examples...I downloaded MSXSL.exe but can't find exempls about using it to transform XML...
Can anyone write a sample?
I have customers.xml like :

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Customers table from Northwind database-->
<Customers>
  <Customer CustomerID="ALFKI">
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>Germany</Country>
    <Orders>
      <Order OrderID="10643">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10692">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10702">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10835">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="10952">
        <ShipCity>Berlin</ShipCity>
      </Order>
      <Order OrderID="11011">
        <ShipCity>Berlin</ShipCity>
      </Order>
    </Orders>
  </Customer>
  <Customer CustomerID="ANATR">
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <Country>Mexico</Country>
    <Orders>
      <Order OrderID="10308">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10625">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10759">
        <ShipCity>México D.F.</ShipCity>
      </Order>
      <Order OrderID="10926">
        <ShipCity>México D.F.</ShipCity>
      </Order>
    </Orders>
  </Customer>
</Customers>

And trying to generate another XML that contains only "country" nodes..
How to write it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

弄潮 2024-10-05 06:39:27

完全相同,您只需要相应地使用 output 元素的 method 属性即可。

It's exactly the same, you just need to use the method attribute of the output element accordingly.

随心而道 2024-10-05 06:39:27

像这样的事情应该有效......

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Countries>
      <xsl:apply-templates select="//Country" mode="copyNode" />
    </Countries>
  </xsl:template>

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

这是结果......

<?xml version="1.0" encoding="utf-8"?>
<Countries>
  <Country>Germany</Country>
  <Country>Mexico</Country>
</Countries>

Something about like this should work...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Countries>
      <xsl:apply-templates select="//Country" mode="copyNode" />
    </Countries>
  </xsl:template>

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

... Here is the result ...

<?xml version="1.0" encoding="utf-8"?>
<Countries>
  <Country>Germany</Country>
  <Country>Mexico</Country>
</Countries>
初见终念 2024-10-05 06:39:27

这里有两种不同的解决方案:一种是简单的解决方案,另一种是仅生成独特国家/地区的更复杂的解决方案:

.1。获取所有 Country 元素:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="*[not(self::Country)]">
  <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时,将生成所需的正确结果

<Country>Germany</Country>
<Country>Mexico</Country>

.2。 查找所有唯一的国家/地区

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="kCountryByVal" match="Country" use="."/>

 <xsl:template match="/">
     <xsl:copy-of select=
       "/*/*/Country[generate-id()
                    =
                     generate-id(key('kCountryByVal',.)[1])
                    ]
       "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于具有所提供文档结构的任何 XML 文档时,其中某些 Country 元素可能具有相同的值,只有一个生成每个不同值的 Country 元素

使用提供的 XML 文档,我们仍然得到相同的结果:

<Country>Germany</Country>
<Country>Mexico</Country>

Here are two different solutions: a simple one and a more complicated one that produces only the unique countries:

.1. Get all Country elements:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="*[not(self::Country)]">
  <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document, the wanted, correct result is produced:

<Country>Germany</Country>
<Country>Mexico</Country>

.2. Find all unique countries:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="kCountryByVal" match="Country" use="."/>

 <xsl:template match="/">
     <xsl:copy-of select=
       "/*/*/Country[generate-id()
                    =
                     generate-id(key('kCountryByVal',.)[1])
                    ]
       "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on any XML document with the structure of the provided document where some Country elements may have the same value, only one Country element for each different value is produced.

With the provided XML document we still get the same result:

<Country>Germany</Country>
<Country>Mexico</Country>
绝情姑娘 2024-10-05 06:39:27

它非常简单,您只需在 xslt 模板中编写 xml 而不是 html 即可。

以下内容(未经测试)应该可以帮助您入门:

<xsl:template match="/">
  <Countries>
    <xsl:apply-templates select="Customers/Customer" />
  </Countries>
</xsl:template>

<xsl:template match="Customer">
  <Country>
    <xsl:value-of select="Country" />
  </Country>
</xsl:template>

另外,对于 XSLT 处理器,我衷心推荐 Visual Studio - 它有一个出色的 XSLT 调试器,它将帮助您无休止地理解样式表。 (我不确定此功能是否包含在 Express 版本中......)

Its pretty simple, you just need to write xml instead of html inside your xslt templates.

The following (untested) should get you started:

<xsl:template match="/">
  <Countries>
    <xsl:apply-templates select="Customers/Customer" />
  </Countries>
</xsl:template>

<xsl:template match="Customer">
  <Country>
    <xsl:value-of select="Country" />
  </Country>
</xsl:template>

Also, for a XSLT processor I heartily recommend Visual Studio - it has an excellent XSLT debugger which will help you out no end in understanding your stylesheets. (I'm not sure if this functionality is included in the express editions...)

攒眉千度 2024-10-05 06:39:27

在拉式样式中,这应该是表达此转换的较短样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="@*|/*|Country|Country/text()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出:

<Customers>
    <Country>Germany</Country>
    <Country>Mexico</Country>
</Customers>

In pull style, this should be the shorter stylesheet expressing this transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="@*|/*|Country|Country/text()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<Customers>
    <Country>Germany</Country>
    <Country>Mexico</Country>
</Customers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文