选择/匹配命名空间中的元素 [英] Selecting/matching elements in a namespace

查看:26
本文介绍了选择/匹配命名空间中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要生成另一个 xml 的 xml 和 xsl,但是由于 'xmlns',它生成了一个空的 xml.在 XML 的根目录中没有 'xmlns',它工作正常.下面的 XSL 还不是一个完整的版本.我需要在其中加入更多逻辑,但由于xmlns",我被困在这里.

I have a xml and xsl that need to generate another xml, but because of 'xmlns', it generates an empty xml. Without 'xmlns' in the root of XML, it works fine. The following XSL is not a complete version yet. I need to put more logic into it, but I am stuck in here, because of 'xmlns'.

我正在使用 Java API 用这个 XML 和 XSL 生成另一个 XML.有人能告诉我为什么它因为 xmlns 而无法匹配元素以及如何修复它吗?

I am using Java API to generate another XML with this XML and XSL. Can somebody tell me why it can't match the element because of the xmlns and how to fix it?

谢谢.

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>

推荐答案

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

You need to assign a prefix to the namespace and use that prefix when addressing the elements of the source 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-configsub-config 是两个不同的东西.


Note that sub-config and subi-config are two different things.

这篇关于选择/匹配命名空间中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆