将多个命名空间添加到 xml 文件

发布于 2024-11-07 03:00:42 字数 2373 浏览 1 评论 0原文

我有一个 xml 文件,并且 xml 文件中给出了命名空间。我需要做的是仅使用 xml 文件中给出的命名空间并限定 xml 文件。下面是示例 xml 文件。

<Ticketing xmlns="ticketing.4.0" mfAction="BOOKING">
  <Reference>
    <Code>190</Code>
  </Reference>
  <BookingID>194283532</BookingID>
  <BookingCode>MCHOI190</BookingCode>
  <BookingDate>2011-04-21T15:40:04.000</BookingDate>
  <Persons>
    <Person>
      <Code>ADULT</Code>
      <Count>2</Count>
    </Person>
    <Person>
      <Code>CHILD</Code>
      <Count>2</Count>
    </Person>
  </Persons>
  <CreditCards>
    <CreditCard BookingType="BOOKING">
      <BookCreditCard xmlns="creditcard.3.0">
        <CardCode>VS</CardCode>
        <CardNumber>4444333322221111</CardNumber>
        <CardExpire>2011-12-31</CardExpire>
      </BookCreditCard>
    </CreditCard>
  </CreditCards>
</Ticketing>

我必须使用 xml 文件中已有的命名空间,并为其指定前缀,并使用这些命名空间限定 xml。输出应如下所示:-

<ticket:Ticketing xmlns:ticket="ticketing.4.0" mfAction="BOOKING">
  <ticket:Reference>
    <ticket:Code>190</ticket:Code>
  </ticket:Reference>
  <ticket:BookingID>194283532</ticket:BookingID>
  <ticket:BookingCode>MCHOI190</ticket:BookingCode>
  <ticket:BookingDate>2011-04-21T15:40:04.000</ticket:BookingDate>
  <ticket:Persons>
    <ticket:Person>
      <ticket:Code>ADULT</ticket:Code>
      <ticket:Count>2</ticket:Count>
    </ticket:Person>
    <ticket:Person>
      <ticket:Code>CHILD</ticket:Code>
      <ticket:Count>2</ticket:Count>
    </ticket:Person>
  </ticket:Persons>
  <ticket:CreditCards>
    <ticket:CreditCard BookingType="BOOKING">
      <credit:BookCreditCard xmlns:credit="creditcard.3.0">
        <credit:CardCode>VS</credit:CardCode>
        <credit:CardNumber>4444333322221111</credit:CardNumber>
        <credit:CardExpire>2011-12-31</credit:CardExpire>
      </credit:BookCreditCard>
    </ticket:CreditCard>
  </ticket:CreditCards>
</ticket:Ticketing>

有人可以建议如何实现这一点。 谢谢 楼陀罗

I have a xml file and namespaces are given in the xml file. What I need to do is to use the namespaces given in the xml file only and qualify the xml file. Below is the sample xml file.

<Ticketing xmlns="ticketing.4.0" mfAction="BOOKING">
  <Reference>
    <Code>190</Code>
  </Reference>
  <BookingID>194283532</BookingID>
  <BookingCode>MCHOI190</BookingCode>
  <BookingDate>2011-04-21T15:40:04.000</BookingDate>
  <Persons>
    <Person>
      <Code>ADULT</Code>
      <Count>2</Count>
    </Person>
    <Person>
      <Code>CHILD</Code>
      <Count>2</Count>
    </Person>
  </Persons>
  <CreditCards>
    <CreditCard BookingType="BOOKING">
      <BookCreditCard xmlns="creditcard.3.0">
        <CardCode>VS</CardCode>
        <CardNumber>4444333322221111</CardNumber>
        <CardExpire>2011-12-31</CardExpire>
      </BookCreditCard>
    </CreditCard>
  </CreditCards>
</Ticketing>

I have to use the namespaces already present in the xml file and give them a prefix and qualify the xml with those namespaces. The output should be like below:-

<ticket:Ticketing xmlns:ticket="ticketing.4.0" mfAction="BOOKING">
  <ticket:Reference>
    <ticket:Code>190</ticket:Code>
  </ticket:Reference>
  <ticket:BookingID>194283532</ticket:BookingID>
  <ticket:BookingCode>MCHOI190</ticket:BookingCode>
  <ticket:BookingDate>2011-04-21T15:40:04.000</ticket:BookingDate>
  <ticket:Persons>
    <ticket:Person>
      <ticket:Code>ADULT</ticket:Code>
      <ticket:Count>2</ticket:Count>
    </ticket:Person>
    <ticket:Person>
      <ticket:Code>CHILD</ticket:Code>
      <ticket:Count>2</ticket:Count>
    </ticket:Person>
  </ticket:Persons>
  <ticket:CreditCards>
    <ticket:CreditCard BookingType="BOOKING">
      <credit:BookCreditCard xmlns:credit="creditcard.3.0">
        <credit:CardCode>VS</credit:CardCode>
        <credit:CardNumber>4444333322221111</credit:CardNumber>
        <credit:CardExpire>2011-12-31</credit:CardExpire>
      </credit:BookCreditCard>
    </ticket:CreditCard>
  </ticket:CreditCards>
</ticket:Ticketing>

Can someone suggest how to implement this.
Thanks
Rudra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2024-11-14 03:00:42

我不确定这是最好的方法。但它确实完成了工作:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output indent="yes"/>

 <xsl:template match="*">
   <xsl:element name="ticket:{name()}" namespace="ticketing.4.0">
    <xsl:apply-templates select="node()|@*"/>
   </xsl:element>
 </xsl:template>

 <xsl:template match="BookCreditCard|BookCreditCard//*"">
  <xsl:element name="credit:{name()}" namespace="creditcard.3.0">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 </xsl:stylesheet>

I'm not sure is the best way to do it. But it does the job:

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output indent="yes"/>

 <xsl:template match="*">
   <xsl:element name="ticket:{name()}" namespace="ticketing.4.0">
    <xsl:apply-templates select="node()|@*"/>
   </xsl:element>
 </xsl:template>

 <xsl:template match="BookCreditCard|BookCreditCard//*"">
  <xsl:element name="credit:{name()}" namespace="creditcard.3.0">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 </xsl:stylesheet>
合久必婚 2024-11-14 03:00:42

在 XSLT 1.0 中,没有有保证的解决方案,因为实现可以自由地在输出文件中使用它们想要的任何前缀。然而,大多数处理器都会做合理的事情,因此 @empo 的解决方案通常会起作用。在 XSLT 2.0 中它保证可以工作。

我倾向于使用名称空间来控制选择哪个模板,如下所示:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ticket="ticketing.4.0" xmlns:credit="creaditcard.3.0">

 <xsl:output indent="yes"/>

 <xsl:template match="ticket:*">
   <xsl:element name="ticket:{local-name()}">
    <xsl:apply-templates select="node()|@*"/>
   </xsl:element>
 </xsl:template>

 <xsl:template match="credit:*">
  <xsl:element name="credit:{local-name()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 </xsl:stylesheet>

In XSLT 1.0 there is no guaranteed solution, because implementations are free to use any prefix in the output file that they want. However most processors do the reasonable thing, and @empo's solution will therefore usually work. In XSLT 2.0 it is guaranteed to work.

I would be inclined to use the namespace to control which template is chosen, something like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ticket="ticketing.4.0" xmlns:credit="creaditcard.3.0">

 <xsl:output indent="yes"/>

 <xsl:template match="ticket:*">
   <xsl:element name="ticket:{local-name()}">
    <xsl:apply-templates select="node()|@*"/>
   </xsl:element>
 </xsl:template>

 <xsl:template match="credit:*">
  <xsl:element name="credit:{local-name()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 </xsl:stylesheet>
治碍 2024-11-14 03:00:42

更通用的解决方案(没有硬编码前缀,可以使用任意数量的命名空间):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="@*">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="*">
  <xsl:element name=
  "{substring(namespace-uri(),1,6)}:{local-name()}"
       namespace="{namespace-uri()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Ticketing xmlns="ticketing.4.0" mfAction="BOOKING">
    <Reference>
        <Code>190</Code>
    </Reference>
    <BookingID>194283532</BookingID>
    <BookingCode>MCHOI190</BookingCode>
    <BookingDate>2011-04-21T15:40:04.000</BookingDate>
    <Persons>
        <Person>
            <Code>ADULT</Code>
            <Count>2</Count>
        </Person>
        <Person>
            <Code>CHILD</Code>
            <Count>2</Count>
        </Person>
    </Persons>
    <CreditCards>
        <CreditCard BookingType="BOOKING">
            <BookCreditCard xmlns="creditcard.3.0">
                <CardCode>VS</CardCode>
                <CardNumber>4444333322221111</CardNumber>
                <CardExpire>2011-12-31</CardExpire>
            </BookCreditCard>
        </CreditCard>
    </CreditCards>
</Ticketing>

生成所需的正确结果

<ticket:Ticketing xmlns:ticket="ticketing.4.0" mfAction="BOOKING">
   <ticket:Reference>
      <ticket:Code>190</ticket:Code>
   </ticket:Reference>
   <ticket:BookingID>194283532</ticket:BookingID>
   <ticket:BookingCode>MCHOI190</ticket:BookingCode>
   <ticket:BookingDate>2011-04-21T15:40:04.000</ticket:BookingDate>
   <ticket:Persons>
      <ticket:Person>
         <ticket:Code>ADULT</ticket:Code>
         <ticket:Count>2</ticket:Count>
      </ticket:Person>
      <ticket:Person>
         <ticket:Code>CHILD</ticket:Code>
         <ticket:Count>2</ticket:Count>
      </ticket:Person>
   </ticket:Persons>
   <ticket:CreditCards>
      <ticket:CreditCard BookingType="BOOKING">
         <credit:BookCreditCard xmlns:credit="creditcard.3.0">
            <credit:CardCode>VS</credit:CardCode>
            <credit:CardNumber>4444333322221111</credit:CardNumber>
            <credit:CardExpire>2011-12-31</credit:CardExpire>
         </credit:BookCreditCard>
      </ticket:CreditCard>
   </ticket:CreditCards>
</ticket:Ticketing>

请注意:每个元素的namespace-uri的前6个字符用于相应生成名称的前缀。因此,只要元素的任何命名空间 uri 的起始 6 个字符遵守 NCName

A more general solution (no hardcoded prefixes and can work with any number of namespaces):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="@*">
  <xsl:copy-of select="."/>
 </xsl:template>

 <xsl:template match="*">
  <xsl:element name=
  "{substring(namespace-uri(),1,6)}:{local-name()}"
       namespace="{namespace-uri()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Ticketing xmlns="ticketing.4.0" mfAction="BOOKING">
    <Reference>
        <Code>190</Code>
    </Reference>
    <BookingID>194283532</BookingID>
    <BookingCode>MCHOI190</BookingCode>
    <BookingDate>2011-04-21T15:40:04.000</BookingDate>
    <Persons>
        <Person>
            <Code>ADULT</Code>
            <Count>2</Count>
        </Person>
        <Person>
            <Code>CHILD</Code>
            <Count>2</Count>
        </Person>
    </Persons>
    <CreditCards>
        <CreditCard BookingType="BOOKING">
            <BookCreditCard xmlns="creditcard.3.0">
                <CardCode>VS</CardCode>
                <CardNumber>4444333322221111</CardNumber>
                <CardExpire>2011-12-31</CardExpire>
            </BookCreditCard>
        </CreditCard>
    </CreditCards>
</Ticketing>

the wanted, correct result is produced:

<ticket:Ticketing xmlns:ticket="ticketing.4.0" mfAction="BOOKING">
   <ticket:Reference>
      <ticket:Code>190</ticket:Code>
   </ticket:Reference>
   <ticket:BookingID>194283532</ticket:BookingID>
   <ticket:BookingCode>MCHOI190</ticket:BookingCode>
   <ticket:BookingDate>2011-04-21T15:40:04.000</ticket:BookingDate>
   <ticket:Persons>
      <ticket:Person>
         <ticket:Code>ADULT</ticket:Code>
         <ticket:Count>2</ticket:Count>
      </ticket:Person>
      <ticket:Person>
         <ticket:Code>CHILD</ticket:Code>
         <ticket:Count>2</ticket:Count>
      </ticket:Person>
   </ticket:Persons>
   <ticket:CreditCards>
      <ticket:CreditCard BookingType="BOOKING">
         <credit:BookCreditCard xmlns:credit="creditcard.3.0">
            <credit:CardCode>VS</credit:CardCode>
            <credit:CardNumber>4444333322221111</credit:CardNumber>
            <credit:CardExpire>2011-12-31</credit:CardExpire>
         </credit:BookCreditCard>
      </ticket:CreditCard>
   </ticket:CreditCards>
</ticket:Ticketing>

Do note: The first 6 characters of the namespace-uri of each element are used for the prefix of the corresponding generated name. Therefore this solution works correctly as long as the starting 6 characters of any namespace-uri of an element obey the sintactical rules for NCName.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文