选择/匹配命名空间中的元素

时间:2014-06-06 16:22:58

标签: xml xslt

我有一个需要生成另一个xml的xml和xsl,但是由于&x; xmlns',它会生成一个空的xml。没有' xmlns'在XML的根目录中,它工作正常。以下XSL尚未完整版本。我需要更多的逻辑,但我被困在这里,因为' xmlns'。

我正在使用Java API通过此XML和XSL生成另一个XML。有人可以告诉我为什么它不能匹配元素因为xmlns以及如何修复它?

感谢。

XML:

<?xml version="1.0" encoding="UTF-8"?>
 <config xmlns="http://xmlns.company.com/config">
   <subi-config>
     <primary-data-source>Repository</primary-data-source>
     <instance>
       <name>agent1</name>
       <address>localhost</address>
       <port>8080</port>
       <admin-username>admin</admin-username>
     </instance>
   </subi-config>
 </config>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://xmlns.company.com/config">

  <xsl:output method="xml" encoding="UTF-8"  indent="yes"/>


  <xsl:template match="/">
        <xsl:apply-templates  select="config"/>
  </xsl:template>


    <xsl:template  match="config">
        <config>
          <sub-config>
        <primary-data-source><xsl:value-of select="sub-config/primary-data-source"/></primary-data-source>
            <xsl:apply-templates select="sub-config"/>
      </sub-config>
    </config>

</xsl:template>


<xsl:template match="sub-config">
        <xsl:for-each select="instance">

            <instance>
              <name><xsl:value-of select="name"/></name>
              <address><xsl:value-of select="address"/></address>
                <port><xsl:value-of select="port"/></port>
              <admin-username><xsl:value-of select="admin-username"/></admin-username>
            </instance>

        </xsl:for-each>

</xsl:template>

2 个答案:

答案 0 :(得分:3)

您需要为命名空间分配前缀,并在寻址源XML的元素时使用该前缀:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cfg="http://xmlns.company.com/config"
exclude-result-prefixes="cfg"> 

<xsl:output method="xml" encoding="UTF-8"  indent="yes"/>

<xsl:template match="/">
    <xsl:apply-templates  select="cfg:config"/>
</xsl:template>

<xsl:template  match="cfg:config">
    <config>
        <sub-config>
            <primary-data-source>
                <xsl:value-of select="cfg:sub-config/cfg:primary-data-source"/>
            </primary-data-source>
            <xsl:apply-templates select="cfg:sub-config"/>
        </sub-config>
    </config>
</xsl:template>

<xsl:template match="cfg:sub-config">
    <xsl:for-each select="cfg:instance">
        <instance>
            <name><xsl:value-of select="cfg:name"/></name>
            <address><xsl:value-of select="cfg:address"/></address>
            <port><xsl:value-of select="cfg:port"/></port>
            <admin-username><xsl:value-of select="cfg:admin-username"/></admin-username>
        </instance>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

请注意,sub-configsubi-config是两回事。

答案 1 :(得分:-1)

将xsl:element与namespace属性

一起使用
<xsl:template  match="config">
  <xsl:element namespace="http://xmlns.company.com/config" name="config">
     <sub-config>
       <primary-data-source><xsl:value-of select="sub-config/primary-data-source"/></primary-data-source>
       <xsl:apply-templates select="sub-config"/>
    </sub-config>
  </xsl:element>

</xsl:template>

它将创建具有计算名称的元素(对我们来说是静态)并允许添加名称空间属性

相关问题