如何定位命名空间 xsi:type="test1"

How to target namespace xsi:type="test1"

下面是我将用于转换的示例输入 XML 文件。转换后需要是一个XML输出

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
    <title>Eros</title>
    <artist>Eros Ramazzotti</artist>
    <country>EU</country>
    <company>BMG</company>
    <price>9.90</price>
    <year>1997</year>
  </cd>
</catalog>

我需要编写一个 XSL 转换器,这样我就可以只循环遍历具有 xsi:type="test1"

的 catalog/cd

输出 XML 应该如下所示

<catalog>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>

非常感谢任何帮助。

干杯

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd[@xsi:type='test2']">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

已解决

请尝试以下解决方案。

XSLT 遵循所谓的身份转换 模式。

输入XML

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test1">
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="test2">
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
</catalog>

XSLT

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="cd[@xsi:type!='test1']"/>
</xsl:stylesheet>

输出XML

<catalog>
  <cd xsi:type="test1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd xsi:type="test1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
</catalog>