XSLT манипулирование именем свойства - PullRequest
0 голосов
/ 20 апреля 2020

У меня есть файл XML, который я хотел бы преобразовать в JSON с помощью XSLT. И я хотел бы манипулировать именами свойств вывода JSON внутри XSLT. Но я не смог этого сделать. Вот что я попробовал ... У меня есть следующие XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="xslt-trial.xsl"?>
<Invoice
  xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
  xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
  xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
  <cbc:AccountingCost>Cost Center</cbc:AccountingCost>
  <cbc:BuyerReference>Buyer reference</cbc:BuyerReference>
  <cac:InvoicePeriod>
    <cbc:StartDate>2020-02-11</cbc:StartDate>
    <cbc:EndDate>2020-02-21</cbc:EndDate>
  </cac:InvoicePeriod>
  <cac:ContractDocumentReference>
    <cbc:ID>Agreement Id</cbc:ID>
  </cac:ContractDocumentReference>
</Invoice>

и "xslt-trial.xsl":

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>

    <!-- Object or Element Property-->
    <xsl:template match="*">
        "<xsl:value-of select="name()"/>" :<xsl:call-template name="Properties">
        <xsl:with-param name="parent" select="'Yes'"> </xsl:with-param>
    </xsl:call-template>
    </xsl:template>

    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:param name="parent"></xsl:param>
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>
            <xsl:when test="not(*|@*)"><xsl:choose><xsl:when test="$parent='Yes'"> <xsl:text>&quot;</xsl:text><xsl:value-of select="."/><xsl:text>&quot;</xsl:text></xsl:when>
                <xsl:otherwise>"<xsl:value-of select="name()"/>":"<xsl:value-of  select="."/>"</xsl:otherwise>
            </xsl:choose>
            </xsl:when>
            <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of  select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
            <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="*"/>
                }</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling::*">,</xsl:if>
    </xsl:template>

    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
</xsl:stylesheet>

Я получаю этот вывод из браузера Edge :

{

        "Invoice" :{

        "cbc:AccountingCost" :"Cost Center",
        "cbc:BuyerReference" :"Buyer reference",
        "cac:InvoicePeriod" :{

        "cbc:StartDate" :"2020-02-11",
        "cbc:EndDate" :"2020-02-21"
                },
        "cac:ContractDocumentReference" :{

        "cbc:ID" :"Agreement Id"
                }
                }}

Что я хочу получить:

{

        "Invoice" :{

        "AccountingCost" :"Cost Center",
        "BuyerReference" :"Buyer reference",
        "InvoicePeriod" :{

        "StartDate" :"2020-02-11",
        "EndDate" :"2020-02-21"
                },
        "ContractDocumentReference" :{

        "ID" :"Agreement Id"
                }
                }}

Как видите, я хотел бы удалить " ca c: " и " cb c: " из имен свойств. Но мне не удается отфильтровать эти значения.

У кого-нибудь есть предложения?

1 Ответ

1 голос
/ 20 апреля 2020

Если вы используете функцию local-name() вместо функции name(), тогда вы должны получить имена элементов без префиксов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...