【问题标题】:XSLT: keep elements of some namesapcesXSLT:保留一些命名空间的元素
【发布时间】:2013-11-16 19:51:30
【问题描述】:

在我的 XML 数据库中,我希望一个名为“commentary”的元素包含三个内容:

  1. 纯文本。
  2. 一些xhtml标签
  3. 一些 TEI 标签(TEI=文本编码初始化)

例子:

<commentary>
<TEI:persName>King Henry iv</TEI:persName> was an <xhtml:b>important</xhtml:b> person.
</commentary>

首先:如何在我的 XML Schema 中声明它? (抱歉,我找到了这个帖子 Allowing certain XHTML tags in an XML Schema? 但它没有帮助我。)

然后(我认为那更复杂)我希望我的 xslt (output:html) 执行以下操作:

将“commentary”的所有内容放入一个“p”元素中,只需取所有xhtml标签,去掉它们的前缀,放入“p”中;删除所有 TEI 标记,但保留其内容。

所以,预期的结果是:

<p>
King Henry iv was an <b>important</b> person.
</p>

【问题讨论】:

    标签: xml xslt xhtml schema


    【解决方案1】:

    这是适用于稍作改动的 xml 的 XSLT

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:TEI="urn:tei" 
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
        exclude-result-prefixes="msxsl xhtml TEI"
    >
        <xsl:output method="html" indent="yes"/>
    
      <xsl:template match="/">
        <html>
          <body>
          <xsl:apply-templates />
          </body>
        </html>
      </xsl:template>
    
        <xsl:template match="commentary">
          <p>
            <xsl:apply-templates />
          </p>
        </xsl:template>
    
      <!-- remove namepace, keep name -->
      <xsl:template match="xhtml:*">
        <xsl:element name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:template>
    
      <!-- surpress nodes in a namespace -->
      <xsl:template match="TEI:*">
        <xsl:value-of select="."/>
      </xsl:template>
    
    </xsl:stylesheet>
    

    输入xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <root xmlns:TEI="urn:tei" xmlns:xhtml="http://www.w3.org/1999/xhtml" >
      <commentary>
        <TEI:persName>King Henry iv</TEI:persName> was an <xhtml:b>important</xhtml:b> person.
      </commentary>
    </root>
    

    这是VS2010根据上面的xml为我生成的schema

    <xs:schema xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:TEI="urn:tei" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:import namespace="urn:tei" />
      <xs:import namespace="http://www.w3.org/1999/xhtml" />
      <xs:element name="root">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="commentary">
              <xs:complexType mixed="true">
                <xs:sequence>
                  <xs:element ref="TEI:persName" />
                  <xs:element ref="xhtml:b" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    TEI 架构

    <xs:schema xmlns:tns="urn:tei" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:tei" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="persName" type="xs:string" />
    </xs:schema>
    

    【讨论】:

      【解决方案2】:

      非常感谢!

      经过几分钟的测试,我发现了一些我能够解决的问题。

      XSLT 不适用于嵌套属性。我通过简单地替换解决了这个问题

          <xsl:value-of select="."/>
      

      <xsl:apply-templates />
      

      属性未处理。我为属性添加了一个 for-each-loop:

      <xsl:for-each select="./@*">
      <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
      </xsl:for-each>
      

      代码现在可以正常工作了

      <?xml version="1.0" encoding="UTF-8"?>
      <?xml-stylesheet type="text/xsl" href="xhtml and tei.xsl"?>
      <root xmlns:tei="urn:tei" xmlns:xhtml="http://www.w3.org/1999/xhtml">
         <commentary>
            <tei:persName>
               <xhtml:b>
                  <xhtml:a href="http://en.wikipedia.or/wiki/Henry_IV_of_England">
                     <xhtml:span style="font-size:20pt">King Henry iv</xhtml:span>
                  </xhtml:a>
               </xhtml:b>
            </tei:persName>
            was an
            <xhtml:b>important</xhtml:b>
            person.
         </commentary>
      </root>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-27
        相关资源
        最近更新 更多