【问题标题】:read xml element by excluding its attribute using XSLT通过使用 XSLT 排除其属性来读取 xml 元素
【发布时间】:2015-11-17 14:01:55
【问题描述】:

我想使用 XSLT 转换给定的 xml 输入,并且我想从转换后的输出中排除根元素的“xmlns”属性

我的输入 xml 是

<?xml version="1.0" encoding="ISO-8859-1"?>

<library xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd">
 <book>
  <title>Programming in C</title>
  <author>Balagurusamy</author>
  <country>India</country>
  <price>165</price>
  <year>1998</year>
 </book>
 <book>
  <title>Professional ASP.NET 4 in C# and VB</title>
  <author>Bill Evjen, Scott Hanselman, Devin Rader</author>
  <country>USA</country>
  <price>580</price>
  <year>2010</year>
 </book>
 <book>
  <title>Professional Microsoft SQL Server 2008 Programming</title>
  <author>Robert Vieira</author>
  <country>USA</country>
  <price>520</price>
  <year>2009</year>
 </book>
 <book>
  <title>Professional LINQ</title>
  <author>Scott Klein</author>
  <country>USA</country>
  <price>425</price>
  <year>2008</year>
 </book>
</library>

使用XSLT转换后预期的xml输出如下

<?xml version="1.0" encoding="UTF-8"?>
<BookList>
<Book>
<title>Programming in C</title>
<author>Balagurusamy</author>
<country>India</country>
</Book>
<Book>
<title>Professional ASP.NET 4 in C# and VB</title>
<author>Bill Evjen, Scott Hanselman, Devin Rader</author>
<country>USA</country>
</Book>

<Book>
<title>Professional Microsoft SQL Server 2008 Programming</title>
<author>Robert Vieira</author>
<country>USA</country>
</Book>
<Book>
<title>Professional LINQ</title>
<author>Scott Klein</author>
<country>USA</country>
</Book>

</BookList>

我的 xslt 是

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:template match="/">
    <BookList>
      <xsl:for-each select="library/book">
        <Book>

          <title>
            <xsl:value-of select="title"/>
          </title>
          <author>
            <xsl:value-of select="author"/>
          </author>
          <country>
            <xsl:value-of select="country" />
          </country>

        </Book>
      </xsl:for-each>
    </BookList>
  </xsl:template>
</xsl:stylesheet>

我需要在上面的 XSLT 中进行哪些更改才能获得给定的预期输出 xml?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    我想从 转换后的输出

    这不是一个属性——它是一个命名空间声明。它将输入 XML 中的所有元素放在绑定到声明的 URI 的命名空间中。

    您需要在样式表中声明此命名空间,为其分配一个前缀,并在输入 XML 中的元素寻址时使用该前缀。没有这个,您的 XPath 表达式将不会选择任何内容。

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns1="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd"
    exclude-result-prefixes="ns1">
    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    
    <xsl:template match="/ns1:library">
        <BookList>
            <xsl:for-each select="ns1:book">
                <Book>
                    <title>
                        <xsl:value-of select="ns1:title"/>
                    </title>
                    <author>
                        <xsl:value-of select="ns1:author"/>
                    </author>
                    <country>
                        <xsl:value-of select="ns1:country" />
                    </country>
                </Book>
            </xsl:for-each>
        </BookList>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 感谢 michael ur XSLT 提供了预期的输出。我们可以让它更通用吗? bcoz 我不知道输入 xml 文件中的命名空间是什么。根据您的回答,我们需要在 xslt 中指定相同的 xmlns。那么有什么方法不需要在 xslt 中包含相同的 xmlns。??
    • @DhirajKhodade 您怎么可能知道输入 XML 的结构,但不知道它使用的命名空间?这不应该改变。
    【解决方案2】:

    我想使用 XSLT 转换给定的 xml 输入,并且我想 从转换后的根元素中排除“xmlns”属性 输出

    我的输入 xml 是

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <library xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/CWLEntityExport.xsd">
     <book>
      <title>Programming in C</title>
     .  .  .
    

    一种非常简短、简单且通用的方法是

    <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output indent="yes"/>
     <xsl:strip-space elements="*"/>
    
      <xsl:template match="*">
        <xsl:element name="{name()}">
          <xsl:copy-of select="namespace::*[name()]"/>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    请注意,不需要在转换中声明命名空间,也不需要特定的 XPath 表达式!

    当此转换应用于提供的 XML 文档(或者,一般来说,应用于需要消除默认命名空间的任何文档)时,会产生所需的正确结果:

    <?xml version="1.0" encoding="utf-8"?>
    <library>
       <book>
          <title>Programming in C</title>
          <author>Balagurusamy</author>
          <country>India</country>
          <price>165</price>
          <year>1998</year>
       </book>
       <book>
          <title>Professional ASP.NET 4 in C# and VB</title>
          <author>Bill Evjen, Scott Hanselman, Devin Rader</author>
          <country>USA</country>
          <price>580</price>
          <year>2010</year>
       </book>
       <book>
          <title>Professional Microsoft SQL Server 2008 Programming</title>
          <author>Robert Vieira</author>
          <country>USA</country>
          <price>520</price>
          <year>2009</year>
       </book>
       <book>
          <title>Professional LINQ</title>
          <author>Scott Klein</author>
          <country>USA</country>
          <price>425</price>
          <year>2008</year>
       </book>
    </library>
    

    更新:

    如果另外需要对元素进行一些重命名,则转换为:

    <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output indent="yes"/>
     <xsl:strip-space elements="*"/>
    
      <xsl:template match="*">
        <xsl:element name="{name()}">
          <xsl:copy-of select="namespace::*[name()]"/>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="*[name()='library']">
        <BookList>
          <xsl:apply-templates/>
        </BookList>
      </xsl:template>
    
      <xsl:template match="*[name()='book']">
        <Book>
          <xsl:apply-templates/>
        </Book>
      </xsl:template>
    
      <xsl:template match="*[name()='price' or name()='year']"/>
    </xsl:stylesheet>
    

    当应用于提供的 XML 文档时,会产生想要的结果:

    <?xml version="1.0" encoding="utf-8"?>
    <BookList>
       <Book>
          <title>Programming in C</title>
          <author>Balagurusamy</author>
          <country>India</country>
       </Book>
       <Book>
          <title>Professional ASP.NET 4 in C# and VB</title>
          <author>Bill Evjen, Scott Hanselman, Devin Rader</author>
          <country>USA</country>
       </Book>
       <Book>
          <title>Professional Microsoft SQL Server 2008 Programming</title>
          <author>Robert Vieira</author>
          <country>USA</country>
       </Book>
       <Book>
          <title>Professional LINQ</title>
          <author>Scott Klein</author>
          <country>USA</country>
       </Book>
    </BookList>
    

    【讨论】:

    • 这不是想要的结果。
    • 感谢@Dimitre,您的解决方案完美运行.. 但此外,我不想选择从输入到转换后的 xml 的所有元素。如您所见,我想从在最终输出中输入 xml。这可能是一个愚蠢的问题 :( 但我从未在 XSLT 上工作过。
    • @DhirajKhodade,只需添加此模板:`
    • @DhirajKhodade,我更新了答案,以便它产生想要的重命名元素并排除年份和价格
    • @Dimitre 太棒了 :) 非常感谢...!
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多